Syntactic Sugar for Common Scenarios

Learn how to leverage ScalaTest’s syntactic sugar to test Scala’s Option, Either, and PartialFunction.

In this lesson, we’ll look into some syntactic sugar to make assertions on Option, Either, and partial functions. So far, we’ve frequently had to deal with Option and Either. In those cases, we often relied on pattern matching or explicit if expressions. There’s nothing wrong with using pattern matching or if in those cases, but in this lesson, we’ll analyze the idiomatic ScalaTest method.

Assertions on Option and Either

The simplest way to assert that an instance of Option[T] contains a valid value, different than None, is simply calling .get on it (line 16).

Get hands-on with 1200+ tech skills courses.