Giovanni Azua Giovanni Azua - 4 months ago 13
Scala Question

How to use ShouldVerb with Double types?

I am new to using the fully fledged scalatest and the relatively new

in version 3.0.0 (ex
). What I'd like to test is something along the lines of the following:

import org.scalatest.FunSuite
import org.scalatest.words.ShouldVerb

class MyFunTest extends FunTest with ShouldVerb {
test("the world makes sense") {
math.abs(1.0 - (1.0 - 1e-10)) should be < 1e-9

but this leads to the compiler error:

value should is not a member of Double

Nio Nio

The docs state that ShouldVerb "Provides an implicit conversion that adds should methods to String to support the syntax of FlatSpec, WordSpec, fixture.FlatSpec, and fixture.WordSpec."

Hence, a Double will not be implicitly decorated with the should verb (only String).

An alternative (but arguably more verbose) assert version below if you want to use the verb style.

import org.scalatest.{ FlatSpecLike, Matchers }

class MathTests extends FlatSpecLike with Matchers {

  "Behavior of math.abs" should "return an absolute number blah blah blah..." in {
    assert(math.abs(1.0 - (1.0 - 1e-10)) < 1e-9)