class Quacky::Stub

Attributes

called_args[R]
expected_args[R]

Public Class Methods

new(method, &block) click to toggle source
# File lib/quacky/stub.rb, line 7
def initialize method, &block
  @method = method
  @return_block = block
end

Public Instance Methods

and_return(value=nil, &block) click to toggle source
# File lib/quacky/stub.rb, line 18
def and_return value=nil, &block
  @return_value = value
  @return_block = block
  self
end
call(*args) click to toggle source
# File lib/quacky/stub.rb, line 24
def call *args
  @called_args = args
  validate_expectation
  call_through *args

  if expected_args
    return_value if (called_args == expected_args)
  else
    return_value
  end
end
validate_satisfaction!() click to toggle source
# File lib/quacky/stub.rb, line 36
def validate_satisfaction!
  if expected_args
    if called_args == expected_args
      true
    else
      raise UnsatisfiedExpectation
    end
  else
    was_called? || raise(UnsatisfiedExpectation, "Expected `#{@method.name}` to be called.")
  end
end
with(*args) click to toggle source
# File lib/quacky/stub.rb, line 12
def with *args
  @expected_args = args
  call_through *args
  self
end

Private Instance Methods

call_through(*args) click to toggle source
# File lib/quacky/stub.rb, line 69
def call_through *args
  begin
    @method.call *args
  rescue ArgumentError => e
    raise Quacky::MethodSignatureMismatch, "#{@method.receiver}##{@method.name} was called with the #{e.message}"
  end
end
return_value() click to toggle source
# File lib/quacky/stub.rb, line 64
def return_value
  return @return_value if @return_value
  @return_block.call if @return_block
end
validate_expectation() click to toggle source
# File lib/quacky/stub.rb, line 55
def validate_expectation
  if expected_args && called_args != expected_args
    raise(
      Quacky::UnexpectedArguments,
      "#{@method.name} was called with unexpected arguments: #{called_args.map(&:inspect).join ", "}. expected: #{expected_args.map(&:inspect).join ", "}"
    )
  end
end
was_called?() click to toggle source
# File lib/quacky/stub.rb, line 51
def was_called?
  !!called_args
end