Class | Assertion |
In: |
lib/quarry/assert.rb
|
Parent: | Exception |
Assertion is a special subclass of Exception used to raise assertion errors.
Assertions are generally invoked via the assert or should functors. For instance to assert that 4 == 4 use:
4.assert == 4
If the assertion does not hold true an Assertion exception will be raised.
You can also create your own assertion "macros" simply by defining applicable methods resuing previous assertion methods.
class Module def should_be_enumerable(module) should < Enumerable instance_methods.should.include?('each') end end
As fancy as assertion functors may seem, under the hood they translate into quite simple code. Consider this valid macro:
def assert_fail_every_time raise Assertion, "this will fail every time" end
Not very useful, but completely valid.
Unfortunately there‘s a bit of rift in the naming scheme of assertion methods. With TDD the term ‘assert’ became the standard. However, with the advent of BDD, ‘should’ has become the dominant term.
If I had my druthers I would probably have picked ‘must’ since it is short and carries the semantics of enforcement.