似乎对于最新版本的 ScalaTest,我们有两个匹配器,一个是Matchers
(之前的ShouldMatchers)和MustMatchers
。
这两者之间的真正区别是什么?是团队可以选择的那种风格吗?
此外,shouldBe
和 shouldBe
最佳答案
唯一的区别是单词 - 引用 t he ScalaDocs for MustMatcher
:
Trait
MustMatchers
is an alternative toMatchers
that provides the exact same meaning, syntax, and behavior asMatchers
, 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.
至于 should[Be]
与 shouldBe
...这取决于您正在调用的 should[Be]
实例。在所有情况下,shouldBe
都会少做一个注册步骤(因为它跳过了 be
所做的工作)。有时看起来这意味着分配的对象更少 - 其他时候,采取相同的步骤,但“应该”和“应该”都在一次通过中注册。据我所知,行为(至少在日常编写测试中)没有区别,只是你在风格上更喜欢哪一种。
关于ScalaTest Matchers 与 MustMatchers 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32353662/