class MockIt::Mock

Public Class Methods

new() click to toggle source
# File lib/rscm/mockit.rb, line 54
def initialize
  @expected_methods=[]
  @expected_validation_procs=[]
  @setup_call_procs={}
  @unexpected_calls = []
end

Public Instance Methods

__expect(method, &validation_proc) click to toggle source
# File lib/rscm/mockit.rb, line 61
def __expect(method, &validation_proc)
  validation_proc=Proc.new {|*args| nil} if validation_proc.nil?
  @expected_methods<<method
  @expected_validation_procs<<validation_proc
  self
end
__setup(method, &proc) click to toggle source
# File lib/rscm/mockit.rb, line 68
def __setup(method, &proc)
  proc=Proc.new {|*args| nil} if proc.nil?
  @setup_call_procs[method]=proc
  self
end
__verify() click to toggle source
# File lib/rscm/mockit.rb, line 74
def __verify
  begin
    assert_no_unexpected_calls
    assert_all_expected_methods_called
  ensure
    initialize
  end
end
method_missing(method, *args, &proc) click to toggle source
# File lib/rscm/mockit.rb, line 83
def method_missing(method, *args, &proc)
  if(is_expected_call(method))
    handle_expected_call(method, *args, &proc)
  elsif(is_setup_call(method))
    handle_setup_call(method, *args, &proc)
  else
    handle_unexpected_call(method)
  end
end
respond_to?(method) click to toggle source
Calls superclass method
# File lib/rscm/mockit.rb, line 93
def respond_to?(method)
  return super.respond_to?(method) if super.respond_to?(method)
  method = symbol(method)
  return true if is_setup_call(method)
  return true if currently_expected_method == method
  false
end

Private Instance Methods

assert_all_expected_methods_called() click to toggle source
# File lib/rscm/mockit.rb, line 112
def assert_all_expected_methods_called
  assert(@expected_validation_procs.empty?, "not all expected methods called, calls left: #{@expected_methods.inspect}")
end
assert_no_unexpected_calls() click to toggle source
# File lib/rscm/mockit.rb, line 108
def assert_no_unexpected_calls
  assert_equal([], @unexpected_calls, "got unexpected call")
end
current_validation_proc() click to toggle source
# File lib/rscm/mockit.rb, line 145
def current_validation_proc
  if @expected_validation_procs.empty? then nil 
  else @expected_validation_procs[0] end
end
currently_expected_method() click to toggle source
# File lib/rscm/mockit.rb, line 140
def currently_expected_method
  if @expected_methods.empty? then nil 
  else @expected_methods[0] end
end
handle_expected_call(method, *args, &proc) click to toggle source
# File lib/rscm/mockit.rb, line 128
def handle_expected_call(method, *args, &proc)
  assert_equal(currently_expected_method, method, "got unexpected call")
  validation_proc = current_validation_proc
  next_call
  validation_proc.call(*args, &proc)
end
handle_setup_call(method, *args, &proc) click to toggle source
# File lib/rscm/mockit.rb, line 124
def handle_setup_call(method, *args, &proc)
  @setup_call_procs[method].call(*args, &proc)
end
handle_unexpected_call(method) click to toggle source
# File lib/rscm/mockit.rb, line 135
def handle_unexpected_call(method)
  @unexpected_calls << method
  flunk("Unexpected method invocation: #{method}")
end
is_expected_call(method) click to toggle source
# File lib/rscm/mockit.rb, line 116
def is_expected_call(method)
  @expected_methods.index(method)
end
is_setup_call(method) click to toggle source
# File lib/rscm/mockit.rb, line 120
def is_setup_call(method)
  not @setup_call_procs[method].nil?
end
next_call() click to toggle source
# File lib/rscm/mockit.rb, line 150
def next_call
  @expected_methods.delete_at(0)
  @expected_validation_procs.delete_at(0)
end
symbol(string) click to toggle source
# File lib/rscm/mockit.rb, line 103
def symbol(string)
  return nil if string==""
  if string.is_a? String then string.intern else string end
end