Matchers
Just like ScalaTest, the Matchers in Specs2 serve the purpose of verifying the results against the expectations. The results of the tests are compared with some expected values, and this is done using Matchers. This is the classic arrange-act-assert archetype.
The most simple example would be a specification for an object that returns the full name of an employee:
// describe the functionality s2"the getFullName method should return full name of the employee $e1" // give an example with some code def e1 = Employee.getFullName() must beEqualTo("Khushboo Maken Sood")
Here, the must
operator will take the result returned by the getFullName
method and pass it to the Matcher, which in this case is beEqualTo
.
Let's look at different types of Matchers ...
Get Scala Test-Driven Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.