Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there syntax for adding a clue to ScalaTest matchers?

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")
like image 305
Daryl Odnert Avatar asked Feb 03 '15 19:02

Daryl Odnert


People also ask

Should matchers vs must matchers?

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.

What is FlatSpec in Scala?

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.


1 Answers

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.

like image 146
gzm0 Avatar answered Oct 27 '22 01:10

gzm0