在ScalaTest中,您可以使用assertResult(expected) { actual }
宏区分断言中的预期值和实际值,如下所示:
Expected X, but got Y
这将打印" X did not equal Y
"测试失败时的消息,而不是通常的" should
"。
你如何使用(must
,{{1}}等来实现类似的事情?)Matchers?
答案 0 :(得分:1)
匹配器会构建自己的错误消息,因此对于标准匹配器,您只需要知道实际值是左侧的值。如果您想更改消息,我相信您必须编写http://www.scalatest.org/user_guide/using_matchers#usingCustomMatchers中的自定义匹配器(以下示例忽略TripleEquals
等):
trait MyMatchers {
class MyEqualMatcher[A](expectedValue: A) extends Matcher[A] {
def apply(left: A) = {
MatchResult(
left == expectedValue,
s"""Expected $expectedValue, but got $left""",
s"""Got the expected value $expectedValue"""
)
}
}
def equalWithMyMessage[A](expectedValue: A) = new MyEqualMatcher(expectedValue) // or extend Matchers and override def equal
}
// in test code extending the trait above
x should equalWithMyMessage(y)