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

How to use ScalaTest Matcher to Test Failure Exception Type?

给定一个可以产生失败的 scala 函数:

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 matchers来测试Exception类型?

我正在寻找以下形式的内容:

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]
}