我的项目中包含所有代码:

package fileSearcher

import org.scalatest.FlatSpec

class FilterCheckerTests extends org.scalatest.FlatSpec {
  "Foo" should
  "not do terrible things" in {

    assert(1 == 1)
  }
}

sbt测试因scala.MatchError而崩溃(以下完整详细信息)。

我究竟做错了什么?
[info] Compiling 1 Scala source to C:\scala\course\FileSearcher\target\scala-2.1
0\test-classes...
[error]
[error]      while compiling: C:\scala\course\FileSearcher\src\test\scala\fileSe
archer\FilterCheckerTests.scala
[error]         during phase: typer
[error]      library version: version 2.10.4
[error]     compiler version: version 2.10.4
[error]   reconstructed args: -classpath C:\scala\course\FileSearcher\target\sca
la-2.10\test-classes;C:\scala\course\FileSearcher\target\scala-2.10\classes;C:\U
sers\Max\.ivy2\cache\org.scalatest\scalatest_2.11\bundles\scalatest_2.11-2.2.4.j
ar;C:\Users\Max\.ivy2\cache\org.scala-lang\scala-reflect\jars\scala-reflect-2.11
.2.jar;C:\Users\Max\.ivy2\cache\org.scala-lang.modules\scala-xml_2.11\bundles\sc
ala-xml_2.11-1.0.2.jar;C:\Users\Max\.ivy2\cache\com.novocode\junit-interface\jar
s\junit-interface-0.11.jar;C:\Users\Max\.ivy2\cache\junit\junit\jars\junit-4.11.
jar;C:\Users\Max\.ivy2\cache\org.hamcrest\hamcrest-core\jars\hamcrest-core-1.3.j
ar;C:\Users\Max\.ivy2\cache\org.scala-sbt\test-interface\jars\test-interface-1.0
.jar -bootclasspath C:\Program Files\Java\jdk1.8.0_20\jre\lib\resources.jar;C:\P
rogram Files\Java\jdk1.8.0_20\jre\lib\rt.jar;C:\Program Files\Java\jdk1.8.0_20\j
re\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\jsse.jar;C:\Prog
ram Files\Java\jdk1.8.0_20\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_20\jre
\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\jfr.jar;C:\Program F
iles\Java\jdk1.8.0_20\jre\classes;C:\Users\Max\.ivy2\cache\org.scala-lang\scala-
library\jars\scala-library-2.10.4.jar
[error]
[error]   last tree to typer: Literal(Constant(true))
[error]               symbol: null
[error]    symbol definition: null
[error]                  tpe: Boolean(true)
[error]        symbol owners:
[error]       context owners: value <local FilterCheckerTests> -> class FilterCh
eckerTests -> package fileSearcher
[error]
[error] == Enclosing template or block ==
[error]
[error] Template( // val <local FilterCheckerTests>: <notype> in class FilterChe
ckerTests
[error]   "org.scalatest.FlatSpec" // parents
[error]   ValDef(
[error]     private
[error]     "_"
[error]     <tpt>
[error]     <empty>
[error]   )
[error]   // 2 statements
[error]   DefDef( // def <init>(): fileSearcher.FilterCheckerTests in class Filt
erCheckerTests
[error]     <method>
[error]     "<init>"
[error]     []
[error]     List(Nil)
[error]     <tpt> // tree.tpe=fileSearcher.FilterCheckerTests
[error]     Block( // tree.tpe=Unit
[error]       Apply( // def <init>(): org.scalatest.FlatSpec in class FlatSpec,
tree.tpe=org.scalatest.FlatSpec
[error]         FilterCheckerTests.super."<init>" // def <init>(): org.scalatest
.FlatSpec in class FlatSpec, tree.tpe=()org.scalatest.FlatSpec
[error]         Nil
[error]       )
[error]       ()
[error]     )
[error]   )
[error]   Apply(
[error]     "Foo".should("not do terrible things")."in"
[error]     Apply(
[error]       "assert"
[error]       Apply( // def ==(x: Int): Boolean in class Int, tree.tpe=Boolean(t
rue)
[error]         1."$eq$eq" // def ==(x: Int): Boolean in class Int, tree.tpe=(x:
 Int)Boolean
[error]         1
[error]       )
[error]     )
[error]   )
[error] )
[error]
[error] == Expanded type of tree ==
[error]
[error] ConstantType(value = Constant(true))
[error]
[error] uncaught exception during compilation: scala.MatchError
[trace] Stack trace suppressed: run last test:compile for the full output.
[error] (test:compile) scala.MatchError: false (of class scala.reflect.internal.
Trees$Literal)
[error] Total time: 0 s, completed Jun 20, 2015 11:07:15 AM
1. Waiting for source changes... (press enter to interrupt)

最佳答案

通过查看由编译器打印的类路径可以看到,您将Scala 2.10与2.11的库混合在一起。鉴于Scala的主要版本是二进制不兼容的,所以这永远都行不通。

这可以通过scalaVersion := "2.11.5"或通过将所有依赖项设置为使用2.10版本来解决,这可以通过libraryDependencies += "group" %% "libName" % "version"完成,其中%%表示sbt自动使用正确的库版本。

关于scala - 为什么我的scalatest测试无法编译? (scala.MatchError),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30955539/

10-11 08:17