如何在ScalaTest中匹配实例的类型
我试图在测试中使用Matchers(不要问为什么)断言异常类型,我得到的解决方案是:如何在ScalaTest中匹配实例的类型,scala,scalatest,matcher,Scala,Scalatest,Matcher,我试图在测试中使用Matchers(不要问为什么)断言异常类型,我得到的解决方案是: exception.getClass shouldBe classOf[FileNotFoundException] 但它看起来超级丑陋,有更好的办法吗 再见一种可能的解决方案是使用拦截方法: val exception = intercept[NoSuchElementException] { List.empty[String].head // Code that throws exception }
exception.getClass shouldBe classOf[FileNotFoundException]
但它看起来超级丑陋,有更好的办法吗
再见一种可能的解决方案是使用
拦截
方法:
val exception = intercept[NoSuchElementException] {
List.empty[String].head // Code that throws exception
}
exception.getMessage shouldBe "head of empty list"
您可以使用“an[]应被丢弃”匹配器:
确保您的测试类包含“匹配器”:
an [IllegalArgumentException] should be thrownBy {
//code that should raise an exception here
}
class MyTestClass extends FunSuite with Matchers