Matcher is only as good as its last matching #1206
-
object AXTests extends Specification {
def is: SpecStructure = s2"""
test assertions $e1
"""
def e1: MatchResult[Any] = {
val t = 1
val r = 3
t must beEqualTo(4) //ignores the MatchFailure
r must beEqualTo(3)
}
} This will pass as it seems the matcher will ignore the first match failure (the equals to 4) and will pass the test cause the last one match. What Am I missing here ? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi @obakamai. You're correct! By default, specs2 is a functional library. At its core no exceptions are thrown.
This means that the default way to check 2 expectations in a default specification is to write: (t must beEqualTo(4)) and
(r must beEqualTo(3)) You can also reuse the fact that import org.specs2.fp.syntax.*
List(
1 must beEqualTo(1),
2 must beEqualTo(4),
3 must beEqualTo(3),
).sumAll This will return: [info] x e2
[error] 2 != 4 You might say that it is wasteful because this will evaluate all the assertions. But this can be an advantage too! Because, with a different import org.specs2.fp.*
import org.specs2.fp.syntax.*
import org.specs2.execute.*
given Monoid[Result] = Result.ResultFailuresMonoid(separator = "\n")
List(
1 must beEqualTo(2),
2 must beEqualTo(2),
3 must beEqualTo(4),
).sumAll This will print: [error] 1 != 2
[error] 3 != 4 You can also go back to a more traditional style of writing assertions, with a short-circuiting behaviour. In order to do this, extend your specification with the |
Beta Was this translation helpful? Give feedback.
-
Hi @etorreborre thanks for you reply, |
Beta Was this translation helpful? Give feedback.
That's Scala 3 syntax. To make it Scala 2 replace
*
with_
andgiven Monoid[Result]
withimplicit val resultMonoidInstance: Monoid[Result]