ttt
ttt

Reputation: 4004

ScalaTest assert and matchers

I used ScalaTest in my Scala Play project. But I have a question here, when to use normal assert(xxx === yyy) and when to use ScalaTest matchers like xxx should be yyy.

Personally I prefer to use assert as it is simple and clean. Also can take advantage of ScalaTest's TypedCheckTrippleEquals support, but matchers can't.

For matchers, so far I only found out one thing matchers can do but not assert, which is Array(1, 2) should equal (Array(1, 2)) succeeds.

Not quite sure which is recommended and are there any other stuff matchers can do more? Otherwise happy to use assert.

Upvotes: 8

Views: 1760

Answers (1)

marios
marios

Reputation: 8996

Here are some things that I love from matchers:

1) Check numbers in a range

sevenDotOh should equal (6.9 +- 0.2)

2) Checking length

result should have length 3

3) Checking for a type

result1 shouldBe a [Tiger] 

4) Checking if an element (or more) belong in a collection

List(1, 2, 3, 4, 5) should contain oneOf (5, 7, 9)
List(1, 2, 3) should contain (2)

5) Various cool tests

List(1, 2, 3) shouldBe sorted

If you have not read this documentation, please do.

Upvotes: 6

Related Questions