3

I'm using specs2 and my understanding is that must and should are equivalent (see What is the difference between should and must in scala testing?), and using one or the other is just a personal preference.

However, when comparing Strings, the following test using must works:

import org.specs2.mutable._

class StringEqualWithMust extends Specification {

  "string comp " should {
    "abc" must beEqualTo("abc")
  }
}

But the same test using should won't compile:

import org.specs2.mutable._

class StringEqualWithShould extends Specification {

  "string comp " should {
    "abc" should beEqualTo("abc")
  }
}

The compilation error is:

StringEqualWithShould.scala:7: overloaded method value should with alternatives:
[error]   (fs: => org.specs2.specification.core.Fragments)(implicit p1: org.specs2.control.ImplicitParameters.ImplicitParam1)org.specs2.specification.core.Fragments <and>
[error]   (f: => org.specs2.specification.core.Fragment)org.specs2.specification.core.Fragment
[error]  cannot be applied to (org.specs2.matcher.BeEqualTo)
[error]     "abc" should beEqualTo("abc")
[error]           ^
[error] one error found

Why is there a difference between must and should when comparing Strings ?

I'm using sbt 0.13.8, scala 2.12.0, and specs2 3.8.6

Community
  • 1
  • 1
obourgain
  • 8,856
  • 6
  • 42
  • 57

1 Answers1

0

The difficulty comes from the fact that should can be used to open a block of examples but also to describe an expectation. You can workaround this by mixing in the following trait

import org.specs2.specification.core._
import org.specs2.control.ImplicitParameters._
import org.specs2.specification.dsl.mutable.ExampleDsl

trait NoShouldBlock extends ExampleDsl {

  override def describe(s: String) = super.describe(s)

  implicit class block(d: String) {
    def >>(f: => Fragment): Fragment = describe(d).>>(f)
    def >>(fs: => Fragments)(implicit p1: ImplicitParam1): Fragments =     describe(d).>>(fs)(p1)
  }

}

And then write your specification like

class StringEqualWithShould extends org.specs2.mutable.Specification with NoShouldBlock {

  "string comp" >> {
    "first example" >> {
      "abc" should beEqualTo("abc")
    }
    "second example" >> {
      "def" should beEqualTo("def")
    }
  }
}
Eric
  • 15,494
  • 38
  • 61