Parent

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

actual_received_count[R]
argument_expectation[R]
error_generator[R]
expectation_ordering[R]
expected_from[R]
expected_received_count[R]
method[R]
method_block[R]

Public Class Methods

new(error_generator, expectation_ordering, expected_from, method, method_block, expected_received_count = 1) click to toggle 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

and_raise(exception = Exception) click to toggle source

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
and_return(value) click to toggle source

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
and_throw(sym) click to toggle source

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
and_yield(*args) click to toggle source

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
invoke(args, block) click to toggle 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
matches(method, args) click to toggle source

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
matches_name_but_not_args(method, args) click to toggle source

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
met?() click to toggle source

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
negative_expectation_for?(method) click to toggle source
# File lib/facon/expectation.rb, line 102
def negative_expectation_for?(method)
  false
end
never() click to toggle source
# File lib/facon/expectation.rb, line 113
def never; times(0); end
once() click to toggle source
# File lib/facon/expectation.rb, line 111
def once; times(1); end
times(val) click to toggle source
# File lib/facon/expectation.rb, line 106
def times(val)
  @expected_received_count = val
  self
end
with(*args, &block) click to toggle source
# File lib/facon/expectation.rb, line 51
def with(*args, &block)
  @method_block = block if block
  @argument_expectation = args
  self
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.