In ScalaTest is there any difference between `should`, `can`, `must`

江枫思渺然 提交于 2019-11-30 00:26:39

问题


Just started using ScalaTest and I quite like it.

By just reading the docs I have thus far been unable to figure out whether there is any substantial difference between the can, should and must clauses for a FlatSpec.

In particular, I'm wondering whether a must failure is treated any differently from a should one - or it's just "syntactic sugar" to make the tests better self-documented.


回答1:


should and must are the same semantically. But it's not about better documentation, it's basically just down to personal stylistic preference (I prefer must for example).

can is a little different. You can't (nomen omen) use it directly as a matcher, it's only available in a test descriptor. Quote from FlatSpec:

Note: you can use must or can as well as should in a FlatSpec. For example, instead of it should "pop..., you could write it must "pop... or it can "pop....

(the same applies for WordSpec and the two corresponding fixture classes)

Note that for a short time (in ScalaTest 2.0.x I think), the use of must was deprecated, however, in 2.1.0, the decision has been reverted:

Resurrected MustMatchers in package org.scalatest. Changed deprecation warning for org.scalatest.matchers.MustMatchers to suggest using org.scalatest.MustMatchers instead of org.scalatest.Matchers, which was the suggestion in 2.0. Apologies to must users who migrated to should already when upgrading to 2.0.



来源:https://stackoverflow.com/questions/22242172/in-scalatest-is-there-any-difference-between-should-can-must

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!