Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assert the type of an object, in specs2

In a specs2 test, how to validate the type of the return value of a function?

Say, the function:

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:

"test" should {
   "return C1 when n is even" in {
       val result = test(2)
       // how to assert
       // 'result' should have type of C1?
   }
}

I want to know how to assert the type of value result?

like image 474
Freewind Avatar asked Jun 03 '14 02:06

Freewind


1 Answers

there is a haveClass matcher:

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]
    }
  }
}
like image 152
stew Avatar answered Oct 12 '22 21:10

stew