我正在尝试使用ScalaTest编写一个基本上声明“它不应该抛出异常,或者抛出可能的异常列表之一”的属性,GeneratorDrivenPropertyChecks
而它又使用scalatest。问题是我无法将noException
与逻辑或结合起来,所以我能想到的最好的就是这个丑陋的测试:
it should "not throw unexpected exceptions" in {
forAll { (s: String) =>
try { parse(s) }
catch { case e:Throwable => e shouldBe a [ParsingFailedException] }
true shouldBe true // prevent compile error
}}
相反,我希望看到的内容更像
it should "not throw unexpected exceptions" in {
forAll { (s: String) => {
(noException should Be thrownBy) or (a [ParsingFailedException] shouldBe thrownBy) { parse(s) }
}}