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

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

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