为什么 sbt 会说“错误的符号引用..."?使用 ScalaTest 进行测试? [英] Why does sbt say "bad symbolic reference..." for test with ScalaTest?

查看:37
本文介绍了为什么 sbt 会说“错误的符号引用..."?使用 ScalaTest 进行测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始将 ScalaTest 与 sbt 一起使用.build.sbt 如下:

I started using ScalaTest with sbt. The build.sbt is as follows:

name := "MySpecSample"

version := "1.0"

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

scalaVersion := "2.10.3" 

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

Initial test code is here. This test code runs alone without main component code.

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()
    } 
  }
}

我是根据官方快速入门编写的.但它不能正常工作.错误信息如下:

I wrote it according to Official quick start. But it doesn't work properly. The error message is as follows:

> 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 上找到相同的问题,但没有好的答案.我的环境在这里.

I can find same problems on Google but no good answer was available. My environment is here.

  • MacOSX 10.9.3
  • Scala 2.10.3
  • sbt 0.13.2

我降级后用sbt 0.13.1试过,结果和0.13.2一样.我应该怎么做才能将 ScalaTest 与 sbt 一起使用?

I tried with sbt 0.13.1 after downgrading, but the result is the same to the one of 0.13.2. What should I do to use ScalaTest with sbt?

推荐答案

在您的 build.sbt 中,您将 Scala 的版本设置为 2.10.3,如下所示:

In your build.sbt you set the version of Scala to 2.10.3 with the following:

scalaVersion := "2.10.3" 

然而 ScalaTest 依赖显式使用 Scala 2.11(注意 _2.11 部分):

However the ScalaTest dependency uses Scala 2.11 explicitly (note the _2.11 part):

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

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

You have to use the same major scala version for both.

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

With sbt you can simply ensure this using %% instead of % as follows:

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

这篇关于为什么 sbt 会说“错误的符号引用..."?使用 ScalaTest 进行测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆