找不到:值assertThrows

oct*_*ian 12 scala scalatest

我有以下测试类:

import org.scalatest.FunSuite

@RunWith(classOf[JUnitRunner])
class NodeScalaSuite extends FunSuite {
Run Code Online (Sandbox Code Playgroud)

有了这个测试方法:

  test("Now doesn't terminate future that's not done") {
    val testFuture: Future[Int] = Future{
      wait(1000)
      10
    }
    assertThrows[NoSuchElementException]{
      testFuture.now
    }
  }
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

not found: value assertThrows
Run Code Online (Sandbox Code Playgroud)

我查看了http://doc.scalatest.org/3.0.0/#org.scalatest.FunSuite上的ScalaTest文档,与我类似的代码似乎工作正常.

有什么问题?

rhi*_*nds 20

我也只是遇到了这个问题 - 但正如评论中所提到的,这是我正在使用的scala测试版本.assertThrows直到2.2.6之后才被引入 - 升级到3.0.0(如果可以的话)将使其成为可能:请参阅旧版本的参考文档:http://doc.scalatest.org/2.2.6/#org. scalatest.FunSuite

如果无法升级,则先前断言异常的intercept方法是使用以下方法:

  test("Invoking head on an empty Set should produce NoSuchElementException") {
    intercept[NoSuchElementException] {
      Set.empty.head
    }
  }
Run Code Online (Sandbox Code Playgroud)