Is there an idiom for adding a clue to a ScalaTest matcher such that the clue will become part of the assertion failure? I know that I can currently write a ScalaTest assertion like this:
withClue("expecting a header row and 3 rows of data") {
rowCount should equal(4)
}
Is this the only syntax for adding the clue to the assertion? It would be nice to be able write an assertion to look something like this:
rowCount should equal(4) because("expecting a header row and 3 rows of data")
Matchers , but uses the verb must instead of should . The two traits differ only in the English semantics of the verb: should is informal, making the code feel like conversation between the writer and the reader; must is more formal, making the code feel more like a written specification.
The main premise behind the FlatSpec trait is to help facilitate a BDD style of development. It's named flat because the structure of the tests we write is unnested in nature. In addition, this trait tries to guide us into writing more focused tests with descriptive, specification-style names.
If you mixin AppendedClues
you can write the withClue
as a suffix:
class TestSuite extends FlatSpec with Matchers with AppendedClues {
3 should equal(4) withClue("expecting a header row and 3 rows of data")
}
Also works without parens of course.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With