Class: MockIt::Mock

Inherits:
Object
  • Object
show all
Includes:
Test::Unit::Assertions
Defined in:
lib/rscm/mockit.rb

Instance Method Summary collapse

Constructor Details

#initializeMock

Returns a new instance of Mock.


54
55
56
57
58
59
# File 'lib/rscm/mockit.rb', line 54

def initialize
  @expected_methods=[]
  @expected_validation_procs=[]
  @setup_call_procs={}
  @unexpected_calls = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &proc) ⇒ Object


83
84
85
86
87
88
89
90
91
# 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

Instance Method Details

#__expect(method, &validation_proc) ⇒ Object


61
62
63
64
65
66
# 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) ⇒ Object


68
69
70
71
72
# 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

#__verifyObject


74
75
76
77
78
79
80
81
# File 'lib/rscm/mockit.rb', line 74

def __verify
  begin
    assert_no_unexpected_calls
    assert_all_expected_methods_called
  ensure
    initialize
  end
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)

93
94
95
96
97
98
99
# 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