Freewind
Freewind

Reputation: 198318

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?

Upvotes: 13

Views: 4140

Answers (1)

stew
stew

Reputation: 11366

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

Upvotes: 14

Related Questions