class Facon::Expectation
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.
Attributes
Public Class Methods
Source
# 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
Public Instance Methods
Source
# File lib/facon/expectation.rb, line 42 def and_raise(exception = Exception) @exception_to_raise = exception end
Sets up the expected method to raise the given exception
(default: Exception).
Source
# 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 return the given value.
Source
# File lib/facon/expectation.rb, line 47 def and_throw(sym) @symbol_to_throw = sym end
Sets up the expected method to throw the given symbol
.
Source
# File lib/facon/expectation.rb, line 35 def and_yield(*args) @args_to_yield << args self end
Sets up the expected method to yield with the given arguments.
Source
# 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
Source
# File lib/facon/expectation.rb, line 92 def matches(method, args) @method == method && check_arguments(args) end
Returns true if the given method
and arguments match this Expectation
.
Source
# File lib/facon/expectation.rb, line 98 def matches_name_but_not_args(method, args) @method == method && !check_arguments(args) end
Returns true if the given method
matches this Expectation
, but the given arguments don’t.
Source
# 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
Returns true if this expectation has been met. TODO at_least and at_most conditions
Source
# File lib/facon/expectation.rb, line 102 def negative_expectation_for?(method) false end
Source
# File lib/facon/expectation.rb, line 106 def times(val) @expected_received_count = val self end
Source
# File lib/facon/expectation.rb, line 51 def with(*args, &block) @method_block = block if block @argument_expectation = args self end
Private Instance Methods
Source
# File lib/facon/expectation.rb, line 116 def check_arguments(args) case @argument_expectation when :any then true when args then true end end
Source
# File lib/facon/expectation.rb, line 123 def invoke_method_block(args) @method_block.call(*args) rescue => e raise_block_failed_error(@method, e.message) end