Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 817
ttt Avatar asked Feb 05 '16 00:02

ttt


1 Answers

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.

like image 82
marios Avatar answered Sep 20 '22 18:09

marios