class Mocha::Invocation

Attributes

block[R]
method_name[R]

Public Class Methods

new(mock, method_name, arguments = [], block = nil) click to toggle source
# File lib/mocha/invocation.rb, line 13
def initialize(mock, method_name, arguments = [], block = nil)
  @mock = mock
  @method_name = method_name
  @arguments = arguments
  @block = block
  @yields = []
  @result = nil
end

Public Instance Methods

arguments() click to toggle source
# File lib/mocha/invocation.rb, line 53
def arguments
  @arguments.dup
end
call(yield_parameters = YieldParameters.new, return_values = ReturnValues.new) click to toggle source
# File lib/mocha/invocation.rb, line 22
def call(yield_parameters = YieldParameters.new, return_values = ReturnValues.new)
  yield_parameters.next_invocation.each do |yield_args|
    @yields << ParametersMatcher.new(yield_args)
    if @block
      @block.call(*yield_args)
    else
      raise LocalJumpError unless Mocha.configuration.reinstate_undocumented_behaviour_from_v1_9?
      yield_args_description = ParametersMatcher.new(yield_args).mocha_inspect
      Deprecation.warning(
        "Stubbed method was instructed to yield #{yield_args_description}, but no block was given by invocation: #{call_description}.",
        ' This will raise a LocalJumpError in the future.',
        ' Use Expectation#with_block_given to constrain this expectation to match invocations supplying a block.',
        ' And, if necessary, add another expectation to match invocations not supplying a block.'
      )
    end
  end
  return_values.next(self)
end
call_description() click to toggle source
# File lib/mocha/invocation.rb, line 57
def call_description
  description = "#{@mock.mocha_inspect}.#{@method_name}#{argument_description}"
  description << ' { ... }' unless @block.nil?
  description
end
full_description() click to toggle source
# File lib/mocha/invocation.rb, line 73
def full_description
  "\n  - #{call_description} #{result_description}"
end
raised(exception) click to toggle source
# File lib/mocha/invocation.rb, line 45
def raised(exception)
  @result = RaisedException.new(exception)
end
result_description() click to toggle source
# File lib/mocha/invocation.rb, line 67
def result_description
  desc = "# => #{@result.mocha_inspect}"
  desc << " after yielding #{@yields.map(&:mocha_inspect).join(', then ')}" if @yields.any?
  desc
end
returned(value) click to toggle source
# File lib/mocha/invocation.rb, line 41
def returned(value)
  @result = value
end
short_call_description() click to toggle source
# File lib/mocha/invocation.rb, line 63
def short_call_description
  "#{@method_name}(#{@arguments.join(', ')})"
end
threw(tag, value) click to toggle source
# File lib/mocha/invocation.rb, line 49
def threw(tag, value)
  @result = ThrownObject.new(tag, value)
end

Private Instance Methods

argument_description() click to toggle source
# File lib/mocha/invocation.rb, line 79
def argument_description
  signature = arguments.mocha_inspect
  signature = signature.gsub(/^\[|\]$/, '')
  signature = signature.gsub(/^\{|\}$/, '') if arguments.length == 1
  "(#{signature})"
end