Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of assert_predicate in MiniTest?

Tags:

ruby

minitest

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.

like image 755
delpha Avatar asked Dec 08 '22 01:12

delpha


2 Answers

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 }
like image 78
Chris Kottom Avatar answered Dec 23 '22 10:12

Chris Kottom


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.

like image 32
Shadwell Avatar answered Dec 23 '22 10:12

Shadwell