Assertions are methods that we can use to verify our code. The idea is very simple: We pass an expression that we believe is true to a method called "assert" which then double checks the expression. If the expression is false, #assert stops our program from continuing by throwing an error.
So, the #assert method basically "asserts" that the thing we pass it is true. That's it!
Using the following as a basis, build an assert() method in Ruby:
- assert() takes a test and an optional message
- return true if the test is true
- error with the user's message (or a default) if the test is not true
def assert(test, msg = nil)
...
end
- How does assert behave when not passed a boolean expression?
- For that matter, how what qualifies as "true" in Ruby?
Using your assert() method, build assert_equal (easy!):
- assert_equal takes two expressions and an optional message
- assert_equal returns true iff the two expressions are equal
- assert_equal throws an error with the optional message if the expressions are not equal
- write assert_nil
- write assert_match (takes a regex and a string)
- write assert_includes
- make up your own assertion
We will use asserts to reduce errors by verifying assumptions in our code, and as the basis for automated tests.