An Expectation, also know as a mock method, is an expectation that an object should receive a specific message during the execution of an example.
# File lib/facon/expectation.rb, line 12 def initialize(error_generator, expectation_ordering, expected_from, method, method_block, expected_received_count = 1) @error_generator = error_generator @expectation_ordering = expectation_ordering @expected_from = expected_from @method = method @method_block = method_block @expected_received_count = expected_received_count @argument_expectation = :any @exception_to_raise = nil @symbol_to_throw = nil @actual_received_count = 0 @args_to_yield = [] end
Sets up the expected method to raise the given exception (default: Exception).
# File lib/facon/expectation.rb, line 42 def and_raise(exception = Exception) @exception_to_raise = exception end
Sets up the expected method to return the given value.
# File lib/facon/expectation.rb, line 28 def and_return(value) raise MockExpectationError, 'Ambiguous return expectation' unless @method_block.nil? @return_block = proc { value } end
Sets up the expected method to throw the given symbol.
# File lib/facon/expectation.rb, line 47 def and_throw(sym) @symbol_to_throw = sym end
Sets up the expected method to yield with the given arguments.
# File lib/facon/expectation.rb, line 35 def and_yield(*args) @args_to_yield << args self end
# File lib/facon/expectation.rb, line 57 def invoke(args, block) begin raise @exception_to_raise unless @exception_to_raise.nil? throw @symbol_to_throw unless @symbol_to_throw.nil? return_value = if !@method_block.nil? invoke_method_block(args) elsif @args_to_yield.size > 0 @args_to_yield.each { |curr_args| block.call(*curr_args) } else nil end if defined?(@return_block) && @return_block args << block unless block.nil? @return_block.call(*args) else return_value end ensure @actual_received_count += 1 end end
Returns true if the given method and arguments match this Expectation.
# File lib/facon/expectation.rb, line 92 def matches(method, args) @method == method && check_arguments(args) end
Returns true if the given method matches this Expectation, but the given arguments don't.
# File lib/facon/expectation.rb, line 98 def matches_name_but_not_args(method, args) @method == method && !check_arguments(args) end
Returns true if this expectation has been met. TODO at_least and at_most conditions
# File lib/facon/expectation.rb, line 83 def met? return true if @expected_received_count == :any || @expected_received_count == @actual_received_count raise_expectation_error(self) end
# File lib/facon/expectation.rb, line 102 def negative_expectation_for?(method) false end
Generated with the Darkfish Rdoc Generator 2.