O'Reilly logo

Scala Test-Driven Development by Gaurav Sood

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required