在specs2测试中,如何验证函数返回值的类型?
说,功能:
trait P
trait C1 extends P
trait C2 extends P
def test(n:Int): P = if(n%2==0) new C1 else new C2
测试:
"test" should {
"return C1 when n is even" in {
val result = test(2)
// how to assert
// 'result' should have type of C1?
}
}
我想知道如何断言值result
的类型?
答案 0 :(得分:14)
有一个haveClass
匹配器:
class FooSpec extends Specification {
trait P
class C1 extends P
class C2 extends P
def test(n:Int): P = if(n%2==0) new C1 else new C2
"test" should {
"return C1 when n is even" in {
val result = test(2)
result must haveClass[C1]
}
}
}