ScalaTest 抛出通过测试异常消息

标签 scala scalatest

为了测试我的代码是否抛出预期的异常,我使用以下语法:

  an [IllegalArgumentException] should be thrownBy(Algorithms.create(degree))

有什么方法可以测试抛出的异常是否包含预期的消息,例如:
  an [IllegalArgumentException(expectedMessage)] should be thrownBy(Algorithms.create(degree)) -- what doesn't compile 

?

最佳答案

Documentation :

the [ArithmeticException] thrownBy 1 / 0 should have message "/ by zero"

并使用该示例:
 the [IllegalArgumentException] thrownBy(Algorithms.create(degree)) should have message expectedMessage

关于ScalaTest 抛出通过测试异常消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31215768/

相关文章:

scala - 在 Scalatest 中使用什么代替符号?

scala - 如何减少在 Scala 中创建的对象数量?

data-structures - 在 Scala 中扩展现有的数据结构

scala - 如何从 ScalaTest 获取指示测试套件失败的返回值?

java - 将 Scala 测试集成到现有 Netbeans Java 项目中的最佳方式?

scala - 简洁但可扩展的 Scala/sbt 测试以验证 Project Euler 答案

eclipse - 如何在 Scala IDE 中导入所有未导入的类?

string - 在 Scala 中比较字符串列表

scala - Firebase 和 Play Framework (Scala) 有可能吗?

scala - Scalacheck 中是否有与 delta 等效的 Junit assetEquals