ScalaTest规范编译错误

use*_*354 10 testing dependencies scala

我是Scala的新手,我正在尝试使用ScalaTest.我把它的依赖包含在我的build.sbt文件中 libraryDependencies++=Seq( "org.scalatest" % "scalatest_2.11" % "2.1.7" % "test" )

和刷新sbt现在它出现在我的外部库文件夹中,所以我认为它已正确安装.现在我想做一个测试课.所以我在src/test/scala下创建了一个.我使用了ScalaTest网站首页的例子

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()
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我运行这个类时,我得到了错误

 Error:scalac: bad symbolic reference. A signature in package.class refers to type compileTimeOnly
in package scala.annotation which is not available.
It may be completely missing from the current classpath, or the version on
the classpath might be incompatible with the version used when compiling package.class.
Run Code Online (Sandbox Code Playgroud)

并且

 Error:(4, 27) Reference to class FlatSpec in package scalatest should not have survived past type checking,
it should have been processed and eliminated during expansion of an enclosing macro.
class ExampleSpec extends FlatSpec with Matchers {
                      ^
Run Code Online (Sandbox Code Playgroud)

有人能告诉我问题是什么.看起来它没有识别ScalaTest.然而,它在我的外部库中,IntelliJ的自动完成也表明它在那里.在我真正开始使用ScalaTest之前,我是否需要刷新其他东西?

编辑:

此外,当我test:compile从sbt 运行时,我得到了

  [error] error while loading package, class file needed by package is missing.
[error] reference value <init>$default$2 of object deprecated refers to nonexisting symbol.
[error] error while loading Matchers, class file needed by Matchers is missing.
[error] reference value <init>$default$2 of object deprecated refers to nonexisting symbol.
[error] error while loading Assertions, class file needed by Assertions is missing.
[error] reference value internal of package scala.reflect.macros refers to nonexisting symbol.
[error] error while loading AbstractSuite, class file needed by AbstractSuite is missing.
[error] reference value <init>$default$2 of object deprecated refers to nonexisting symbol.
[error] error while loading Tolerance, class file needed by Tolerance is missing.
[error] reference value <init>$default$2 of object deprecated refers to nonexisting symbol.
[error] error while loading BeWord, class file needed by BeWord is missing.
[error] reference value <init>$default$2 of object deprecated refers to nonexisting symbol.
[error] error while loading ResultOfNotWordForAny, class file needed by ResultOfNotWordForAny is missing.
[error] reference value <init>$default$2 of object deprecated refers to nonexisting symbol.
[error] error while loading NotWord, class file needed by NotWord is missing.
[error] reference value <init>$default$2 of object deprecated refers to nonexisting symbol.
[error] 8 errors found
[error] (test:compile) Compilation failed
Run Code Online (Sandbox Code Playgroud)

Kla*_*aus 15

似乎SBT试图针对scala 2.9.2进行编译.我想你必须加一个

scalaVersion := "2.10.4"
Run Code Online (Sandbox Code Playgroud)

要么

scalaVersion := "2.11.1"
Run Code Online (Sandbox Code Playgroud)

到你的 build.sbt

而且当然

libraryDependencies += "org.scalatest" %% "scalatest" % "2.1.7" % "test"
Run Code Online (Sandbox Code Playgroud)

此外,您可以尝试最新的SBT发射器.