Class: Tiramisu::Assert
- Inherits:
-
Object
- Object
- Tiramisu::Assert
- Defined in:
- lib/tiramisu/assert.rb
Instance Method Summary collapse
- #__validate_expectations__ ⇒ Object
-
#initialize(object, action = :assert, caller = nil) ⇒ Assert
constructor
A new instance of Assert.
-
#method_missing(m, *a, &b) ⇒ Object
forward any missing method to tested object.
-
#raise(type = nil, message = nil, &block) ⇒ Object
(also: #to_raise)
ensure the given block raises as expected.
-
#receive(expected_message) ⇒ Expectation
(also: #to_receive)
ensure given mock will receive expected message by the end of test.
-
#throw(expected_symbol = nil, &block) ⇒ Object
(also: #to_throw)
ensure given block thrown as expected.
Constructor Details
#initialize(object, action = :assert, caller = nil) ⇒ Assert
Returns a new instance of Assert.
4 5 6 7 8 9 10 |
# File 'lib/tiramisu/assert.rb', line 4 def initialize object, action = :assert, caller = nil @object = object @caller = caller @assert = action == :assert @refute = action == :refute @assert || @refute || Kernel.raise(ArgumentError, 'action should be either :assert or :refute') end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *a, &b) ⇒ Object
forward any missing method to tested object.
31 32 33 |
# File 'lib/tiramisu/assert.rb', line 31 def method_missing m, *a, &b __assert__(m, a, b) end |
Instance Method Details
#__validate_expectations__ ⇒ Object
127 128 129 130 |
# File 'lib/tiramisu/assert.rb', line 127 def __validate_expectations__ return unless @__expectation__ @__expectation__.validate end |
#raise(type = nil, message = nil, &block) ⇒ Object Also known as: to_raise
Note:
if block given it will have precedence over arguments
ensure the given block raises as expected
76 77 78 79 80 81 82 |
# File 'lib/tiramisu/assert.rb', line 76 def raise type = nil, = nil, &block failure = Tiramisu.__send__( @assert ? :assert_raised_as_expected : :refute_raised_as_expected, @object, type, , block ) Tiramisu.fail(failure, caller[0]) if failure end |
#receive(expected_message) ⇒ Expectation Also known as: to_receive
ensure given mock will receive expected message by the end of test
121 122 123 124 |
# File 'lib/tiramisu/assert.rb', line 121 def receive Kernel.throw(:__tiramisu_status__, @object[:raised]) if @object[:raised] @__expectation__ = Expectation.new(@object[:returned], .to_sym, @assert, caller[0]) end |
#throw(expected_symbol = nil, &block) ⇒ Object Also known as: to_throw
Note:
if block given it will have precedence over arguments
ensure given block thrown as expected
98 99 100 101 102 103 104 |
# File 'lib/tiramisu/assert.rb', line 98 def throw expected_symbol = nil, &block failure = Tiramisu.__send__( @assert ? :assert_thrown_as_expected : :refute_thrown_as_expected, @object, expected_symbol ? expected_symbol.to_sym : nil, block ) Tiramisu.fail(failure, caller[0]) if failure end |