我在了解MiniTest中assert_predicate
的用处时遇到了问题。它与assert_equal?
有何不同?何时想要使用这个断言?我已经多次遇到它,但并没有真正得到它的确切含义。
答案 0 :(得分:10)
assert_equal
检查预期值和实际值是否相等:
assert_equal "Bender", robot.name
assert_predicate
调用目标对象上的named方法,如果结果是真的则传递:
assert_predicate robot, :bender?
您可以轻松地将此测试编写为:
assert robot.bender?
但assert_predicate
在某些情况下可能更具表现力,在检查多个谓词条件时可能会更好一点:
roles = %i(admin? publisher? operator?)
roles.each {|role| assert_predicate user, role }
答案 1 :(得分:2)
The documentation for the method itself may answer your question:
For testing with predicates. Eg:
assert_predicate str, :empty?
This is really meant for specs and is front-ended by assert_operator:
str.must_be :empty?
That suggests that the reason is to support .must_be
calls rather than providing any specific benefit over assert_equal
or assert
.