Minitest provides some handy methods which allows us to check for falsey/truthy values and actually compare values.
assert
- The first argument passed to this method is whatever we are going to assert is truthy. For example,
assert "Hello"
returns true since"Hello"
is a truthy value. - The second argument passed to this method is an optional error message. If no argument is given, it will default to
Failed Assertion, no message given
. For example,assert nil, "Nil is falsey"
will fail, and the error message printed in the terminal will be"Nil is falsey"
.
- The first argument passed to this method is whatever we are going to assert is truthy. For example,
assert_equal
- This method takes two arguments. As the name of the method indicates, we are asserting an equality of the two given values.
refute
- The first argument passed to this method is whatever we are going to assert is falsey. For example,
refute nil
returns true sincenil
is a falsey value. - The second argument passed to this method is an optional error message. If no argument is given, it will default to
Failed Refutation, no message given
. For example,refute "Hello", "The string Hello is truthy"
will fail, and the error message printed in the terminal will be"The string Hello is truthy"
.
- The first argument passed to this method is whatever we are going to assert is falsey. For example,
refute_equal
- This method takes two arguments. As the name of the method indicates, we are refuting an equality of the two given values.
In the command/query exercises, there's a file adult_test.rb
. The test below is the one we are going to focus on, and the implementation is an if/else statement that returns a string depending on how many drinks the adult has had.
def test_adult_does_not_get_drunk_too_easily
adult = Adult.new
adult.consume_an_alcoholic_beverage
assert adult.sober?, "Still sober."
adult.consume_an_alcoholic_beverage
assert adult.sober?, "Not drunk yet."
adult.consume_an_alcoholic_beverage
refute adult.sober?, "Yeah, OK. The adult is drunk."
adult.consume_an_alcoholic_beverage
refute adult.sober?, "The adult doesn't get more sober from drinking more."
end
In the first two assertions, the second argument will only display if the test fails. To make the test pass, we add some conditions to our if/else statement and return some string. The return value is a string, a truthy value, and the assertion passes.
In the last two assertions, the return value from the sober?
method should be falsey and the optional error message will only display if the test fails. In order to make the test pass, the argument we pass to refute
(the return value from the code adult.sober?
) has to be falsey, therefore when we just return the string "Yeah, OK. The adult is drunk."
, a truthy value, the refutation will fail and our custom error message is printed.
In order to make the two last refutations in the test to pass, we have to return a falsey value. The return value of puts
is nil, so if we add puts
before the string "Yeah, OK. The adult is drunk."
in the if/else statement, the return value of the method will be nil and our test is passing.