繁体   English   中英

如何使用 ScalaTest Matcher 测试失败异常类型?

[英]How to use ScalaTest Matcher to Test Failure Exception Type?

给定一个可能产生故障的 scala function:

def testableFunction(x: Int) : Try[Int] = {
  if(x == 0)
    Failure[Int](new IllegalArgumentException("0 is bad"))
  else if(x == 1)
    Failure[Int](new Exception("1 not so good either"))
  else
    Success(42)
}

如何使用scalatest 匹配器来测试异常类型?

我正在寻找以下形式的东西:

testableFunction(0).failure.type should be IllegalArgumentException

testableFunction(1).failure.type should be Exception

预先感谢您的考虑和回复。

混合TryValues特征:

class ExampleTestSpec extends Matchers with TryValues {
  ...
  testableFunction(0).failure.exception shouldBe a [IllegalArgumentException]
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM