I have a problem understanding the usefulness of assert_predicate
in MiniTest. How is it different from assert_equal?
When would one want to use this assertion? I have came across it many times but did not really get its exact meaning.
assert_equal
checks to see whether the expected and actual values are equal:
assert_equal "Bender", robot.name
assert_predicate
calls the named method on your target object and passes if the result is truthy:
assert_predicate robot, :bender?
You could just as easily write this test as:
assert robot.bender?
But assert_predicate
can be a little more expressive in some situations and can be a little nicer when checking multiple predicate conditions:
roles = %i(admin? publisher? operator?)
roles.each {|role| assert_predicate user, role }
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With