The idiomatic way would be to use the MatcherMacros
trait (in specs2-matcher-extra 2.3.10
)
import org.specs2.mock.Mockito
import org.specs2.matcher._
class TestSpec extends org.specs2.mutable.Specification with Mockito with MatcherMacros {
val collaborator = mock[TargetObject]
"verifying a mock was called" should {
"match if a field on the called parameter was the same" in {
val cut = new ClassUnderTest(collaborator)
cut.execute
there was one(collaborator).call(matchA[FirstParameter].first("first"))
}
}
}
class ClassUnderTest(collaborator: TargetObject) {
def execute = collaborator.call(FirstParameter("first", "second"))
}
case class FirstParameter(first: String, second: String)
trait TargetObject {
def call(parameters: FirstParameter)
}
In the code above matchA
is used to match a FirstParameter
and the first
method on matchA
corresponds to the first
value of the FirstParameter
class. In this example I just pass the expected value but you can also pass another specs2 matcher, startWith("f")
for example, or even a function (s: String) => s must haveSize(5)
.