Jason Nesbitt
Jason Nesbitt

Reputation: 740

ScalaTest - How to verify a collection contains a sub class

With ScalaTest, it's straightforward enough to test that an object is of a particular class with something like:

myPet shouldBe a [Dog]

But what I would like to do is verify that a collection of some base type has at least one of a particular sub type. I imagine the syntax could look something like this but it doesn't work:

myPets should contain a [Cat]

I can achieve the same result with 'exists' and 'shouldBe true' but it's not quite as expressive.

myPets.exists(_.isInstanceOf[Cat]) shouldBe true

Is this what I have to do or is there a feature of ScalaTest that I'm not aware of?

Thank you

Upvotes: 0

Views: 107

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170909

atLeast(1, myPets) shouldBe a [Cat] should work, I believe (can't test at the moment, based just on an example from http://www.scalatest.org/user_guide/using_matchers#inspectorShorthands).

Upvotes: 2

Related Questions