The EitherValue trait

By including the EitherValue trait in our mixin, we get an implicit conversion of the Either monad. This implicit conversion adds a value method to both left and right of Either. This allows us to do a left.value and right.value. The value method either returns the selected value from a defined Either or alternatively throws a TestFailedException in case the Either is not defined.

Using this, we are able to test in a single expression, both that the Either should be left or right (that is, it is defined) and also that it has a certain expected value. Let's look at an example:

firstEither.right.value should be < 5
secondEither.left.value should be ("Big Ploblemo!")

The same can be done using assertions:

assert(firstEither.right.value ...

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.