微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么sbt会说“糟糕的符号引用……”用ScalaTest进行测试?

我开始在sbt上使用 ScalaTest. build.sbt如下:

name := "MySpecsample"

version := "1.0"

libraryDependencies  += "org.scalatest" % "scalatest_2.11" % "2.2.0" % "test"

scalaVersion := "2.10.3"

初始测试代码在这里.此测试代码单独运行,没有主要组件代码.

import collection.mutable.Stack
import org.scalatest._

class ExampleSpec extends FlatSpec with Matchers {

  "A Stack" should "pop values in last-in-first-out order" in {
    val stack = new Stack[Int]
    stack.push(1)
    stack.push(2)
    stack.pop() should be (2)
    stack.pop() should be (1)
  }

  it should "throw NoSuchElementException if an empty stack is popped" in {
    val emptyStack = new Stack[Int]
    a [NoSuchElementException] should be thrownBy {
      emptyStack.pop()
    } 
  }
}

我是根据Official quick start写的.
但它不能正常工作.错误消息如下:

> test
[info] Compiling 1 Scala source to /Users/kaisasak/untitled/target/scala-2.10/test-classes...
[error] bad symbolic reference. A signature in package.class refers to type compileTimeOnly
[error] in package scala.annotation which is not available.
[error] It may be completely missing from the current classpath,or the version on
[error] the classpath might be incompatible with the version used when compiling package.class.
[error] /Users/kaisasak/untitled/src/test/scala/ExampleSpec.scala:6: Reference to class FlatSpec in package scalatest should not have survived past type checking,[error] it should have been processed and eliminated during expansion of an enclosing macro.
[error] class ExampleSpec extends FlatSpec with Matchers {
[error]                         ^
[error] two errors found
[error] (test:compile) Compilation Failed
[error] Total time: 3 s,completed Jun 29,2014 11:20:41 AM

我可以在Google上找到相同的问题,但没有很好的答案.
我的环境就在这里.

> MacOSX 10.9.3
> Scala 2.10.3
> sbt 0.13.2

我在降级后尝试使用sbt 0.13.1,但结果与0.13.2相同.我应该怎么做才能将scalaTest与sbt一起使用?

解决方法

在build.sbt中,使用以下命令将Scala的版本设置为2.10.3:

scalaVersion := "2.10.3"

但是,ScalaTest依赖项显式使用Scala 2.11(请注意_2.11部分):

"org.scalatest" % "scalatest_2.11" % "2.2.0" % "test"

您必须为两者使用相同的主要scala版本.

使用sbt,您可以使用%%而不是%来确保这一点,如下所示:

"org.scalatest" %% "scalatest" % "2.2.0" % "test"

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐