或带有标量测试匹配器的运算符



使用 scalatest 惯用匹配器如何检查类是 A 还是 B 的实例?

我尝试使用或但它不起作用

e shouldBe a [ChannelWriteException] || e shouldBe a [ConnectException]

这里 如何使用 Scala(Test( 进行实例检查,说明如何使用 isInstanceOf,但不知道如何制作 或问候。

您可以将Matcher#should(Matcher[T])Matcher#or[U <: T](Matcher[U])一起使用

  it("test instanceOf A or B") {
    val actual = new Thread()
    actual should (be (a [RuntimeException]) or be (a [Thread]))
  }

如果实际与预期不匹配,则通过正确的消息传递出错,

  it("test instanceOf A or B") {
    val actual = new String()
    actual should (be (a [RuntimeException]) or be (a [Thread]))
  }

错误

"" was not an instance of java.lang.RuntimeException, but an instance of java.lang.String, and 
"" was not an instance of java.lang.Thread, but an instance of java.lang.String

传统方式,(我还在 2000 年代(

val actual = new Thread()
assert(actual.isInstanceOf[RuntimeException] || actual.isInstanceOf[Thread])

参考

http://www.scalatest.org/user_guide/using_matchers#logicalExpressions

无论如何,我们可以使用ShouldMatchers在Scalate中给出两个条件

最新更新