Along with assertion, ScalaTest also supports a domain-specific language (DSL) for expressing assertions in the test. This is achieved using the word should
. This is done by mixing in Matchers:
import org.scalatest._ class PacktSpec extends FlatSpec with Matchers
Alternatively, members of the trait can be imported explicitly:
import Matchers._
ScalaTest has the Matchers trait, which you can mixin to your suite
class to write equality assertions like this:
message should equal ("Hello World")
message
is a variable and can be of any type. In all the preceding cases, if the equality is not held true, then TestFailedException
is thrown. A detail message encapsulated in this exception will explain the problem and why the test failed.
Alternatively, you can mixin MustMatchers
as an alternative to Matchers, which proves same syntax and meaning as the Matchers trait, but uses the verb must
instead of should
.