Class: Tiramisu::Expectation
- Inherits:
-
Object
- Object
- Tiramisu::Expectation
- Defined in:
- lib/tiramisu/expectations.rb,
lib/tiramisu/expectations/with.rb,
lib/tiramisu/expectations/raise.rb,
lib/tiramisu/expectations/throw.rb,
lib/tiramisu/expectations/return.rb
Instance Attribute Summary collapse
-
#expected_message ⇒ Object
readonly
Returns the value of attribute expected_message.
-
#received_messages ⇒ Object
readonly
Returns the value of attribute received_messages.
Class Method Summary collapse
Instance Method Summary collapse
-
#and_raise(type = nil, message = nil, &block) ⇒ Object
ensure received message raises as expected.
-
#and_return(value = nil, &block) ⇒ Object
ensure received message returns expected value.
-
#and_throw(symbol = nil, &block) ⇒ Object
ensure received message throws as expected.
- #assert_message_raised_as_expected ⇒ Object
- #assert_message_thrown_as_expected ⇒ Object
-
#initialize(object, expected_message, assert, caller) ⇒ Expectation
constructor
A new instance of Expectation.
- #validate ⇒ Object
-
#with(*args, &block) ⇒ Object
ensure expected message received with correct arguments.
Constructor Details
#initialize(object, expected_message, assert, caller) ⇒ Expectation
Returns a new instance of Expectation.
17 18 19 20 21 22 23 |
# File 'lib/tiramisu/expectations.rb', line 17 def initialize object, , assert, caller @object = object @expected_message = .to_sym @assert = assert @caller = caller proxify(@object, @expected_message) end |
Instance Attribute Details
#expected_message ⇒ Object (readonly)
Returns the value of attribute expected_message.
15 16 17 |
# File 'lib/tiramisu/expectations.rb', line 15 def @expected_message end |
#received_messages ⇒ Object (readonly)
Returns the value of attribute received_messages.
15 16 17 |
# File 'lib/tiramisu/expectations.rb', line 15 def @received_messages end |
Class Method Details
.restore_object_status(obj) ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/tiramisu/expectations.rb', line 4 def self.restore_object_status obj return unless obj.instance_variable_get(:@__tiramisu__original_methods__) obj.instance_variable_get(:@__tiramisu__original_methods__).each_pair do |n,m| obj.define_singleton_method(n, &m) end obj.instance_variable_set(:@__tiramisu__original_methods__, {}) obj.instance_variable_set(:@__tiramisu__received_messages__, {}) end |
Instance Method Details
#and_raise(type = nil, message = nil, &block) ⇒ Object
if block given it will have precedence over arguments
ensure received message raises as expected
13 14 15 |
# File 'lib/tiramisu/expectations/raise.rb', line 13 def and_raise type = nil, = nil, &block @raise = block || [type, ] end |
#and_return(value = nil, &block) ⇒ Object
if block given it will have precedence over arguments
ensure received message returns expected value
12 13 14 |
# File 'lib/tiramisu/expectations/return.rb', line 12 def and_return value = nil, &block @return = block || value end |
#and_throw(symbol = nil, &block) ⇒ Object
if block given it will have precedence over arguments
ensure received message throws as expected
12 13 14 |
# File 'lib/tiramisu/expectations/throw.rb', line 12 def and_throw symbol = nil, &block @throw = [symbol, block] end |
#assert_message_raised_as_expected ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/tiramisu/expectations/raise.rb', line 17 def return unless @raise if @raise.is_a?(Proc) .find {|log| @raise.call(log[:raised])} || Tiramisu.fail([ 'Looks like :%s message never raised as expected' % , 'See validation block' ], @caller) else .each do |log| next unless f = Tiramisu.assert_raised_as_expected(log, *@raise) Tiramisu.fail(f, log[:caller]) end end end |
#assert_message_thrown_as_expected ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/tiramisu/expectations/throw.rb', line 16 def return unless @throw .each do |log| next unless f = Tiramisu.assert_thrown_as_expected(log, *@throw) Tiramisu.fail(f, log[:caller]) end end |
#validate ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/tiramisu/expectations.rb', line 25 def validate @received_messages = @object.[] || [] return unless @assert end |
#with(*args, &block) ⇒ Object
if block given it will have precedence over arguments
ensure expected message received with correct arguments
15 16 17 18 |
# File 'lib/tiramisu/expectations/with.rb', line 15 def with *args, &block @with = block || args self end |