如何在specs2中断言对象的类型

时间:2014-06-03 02:53:30

标签: scala types assertions specs2

在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的类型?

1 个答案:

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