Module: Hardmock

Included in:
Test::Unit::TestCase
Defined in:
lib/hardmock/method_cleanout.rb,
lib/hardmock.rb,
lib/hardmock/mock.rb,
lib/hardmock/utils.rb,
lib/hardmock/errors.rb,
lib/hardmock/trapper.rb,
lib/hardmock/expector.rb,
lib/hardmock/stubbing.rb,
lib/hardmock/expectation.rb,
lib/hardmock/mock_control.rb,
lib/hardmock/expectation_builder.rb

Overview

:nodoc:

Defined Under Namespace

Modules: MethodCleanout, Stubbing, Utils Classes: DeprecationError, Expectation, ExpectationBuilder, ExpectationError, Expector, Mock, MockControl, ReplacedMethod, StubbedMethod, StubbingError, Trapper, VerifyError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_replaced_methodsObject



177
178
179
# File 'lib/hardmock/stubbing.rb', line 177

def all_replaced_methods
  $all_replaced_methods ||= []
end

.has_replaced_method?(obj, method_name) ⇒ Boolean

Returns:

  • (Boolean)


181
182
183
184
185
186
# File 'lib/hardmock/stubbing.rb', line 181

def has_replaced_method?(obj, method_name)
  hits = all_replaced_methods.select do |replaced|
    (replaced.target.object_id == obj.object_id) and (replaced.method_name.to_s == method_name.to_s)
  end
  return !hits.empty?
end

.included(base) ⇒ Object

Setup auto mock verification on teardown, being careful not to interfere with inherited, pre-mixed or post-added user teardowns.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/hardmock.rb', line 17

def self.included(base) #:nodoc:#
  base.class_eval do 
    # Core of our actual setup behavior
    def hardmock_setup
      prepare_hardmock_control
    end
    
    # Core of our actual teardown behavior
    def hardmock_teardown
      verify_mocks
    end

    # disable until later:
    def self.method_added(symbol) #:nodoc:
    end

    if method_defined?(:setup) then
      # Wrap existing setup
      alias_method :old_setup, :setup
      define_method(:new_setup) do
        begin
          hardmock_setup
        ensure
          old_setup
        end
      end
    else
      # We don't need to account for previous setup
      define_method(:new_setup) do
        hardmock_setup
      end
    end
    alias_method :setup, :new_setup

    if method_defined?(:teardown) then
      # Wrap existing teardown
      alias_method :old_teardown, :teardown
      define_method(:new_teardown) do
        begin
          hardmock_teardown
        ensure
          old_teardown
        end
      end
    else
      # We don't need to account for previous teardown
      define_method(:new_teardown) do
        hardmock_teardown
      end
    end
    alias_method :teardown, :new_teardown

    def self.method_added(method) #:nodoc:
      case method
      when :teardown
        unless method_defined?(:user_teardown)
          alias_method :user_teardown, :teardown
          define_method(:teardown) do
            begin
              new_teardown 
            ensure
              user_teardown
            end
          end
        end
      when :setup
        unless method_defined?(:user_setup)
          alias_method :user_setup, :setup
          define_method(:setup) do
            begin
              new_setup 
            ensure
              user_setup
            end
          end
        end
      end
    end
  end
end

.restore_all_replaced_methodsObject



188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/hardmock/stubbing.rb', line 188

def restore_all_replaced_methods
  all_replaced_methods.each do |replaced|
    unless replaced.target._is_mock?
      backed_up = "_hardmock_original_#{replaced.method_name}"
      if replaced.target.methods.include?(backed_up)
        replaced.target.hm_meta_eval do
          alias_method replaced.method_name.to_sym, backed_up.to_sym 
        end
      end
      replaced.target._clear_mock
    end
  end
  all_replaced_methods.clear
end

.track_replaced_method(replaced_method) ⇒ Object



173
174
175
# File 'lib/hardmock/stubbing.rb', line 173

def track_replaced_method(replaced_method)
  all_replaced_methods << replaced_method
end

Instance Method Details

#clear_expectationsObject

Purge the main MockControl of all expectations, restore all concrete stubbed/mocked methods



158
159
160
161
162
# File 'lib/hardmock.rb', line 158

def clear_expectations
  @main_mock_control.clear_expectations if @main_mock_control
  reset_stubs
  $main_mock_control = nil
end

#create_mocks(*mock_names) ⇒ Object Also known as: create_mock

Create one or more new Mock instances in your test suite. Once created, the Mocks are accessible as instance variables in your test. Newly built Mocks are added to the full set of Mocks for this test, which will be verified when you call verify_mocks.

create_mocks :donkey, :cat # Your test now has @donkey and @cat
create_mock  :dog          # Test now has @donkey, @cat and @dog

The first call returned a hash { :donkey => @donkey, :cat => @cat } and the second call returned { :dog => @dog }

For more info on how to use your mocks, see Mock and Expectation



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/hardmock.rb', line 111

def create_mocks(*mock_names)
  prepare_hardmock_control unless @main_mock_control

  mocks = {}
  mock_names.each do |mock_name|
    raise ArgumentError, "'nil' is not a valid name for a mock" if mock_name.nil?
    mock_name = mock_name.to_s
    mock_object = Mock.new(mock_name, @main_mock_control)
    mocks[mock_name.to_sym] = mock_object
    self.instance_variable_set "@#{mock_name}", mock_object
  end
  @all_mocks ||= {}
  @all_mocks.merge! mocks

  return mocks.clone
end

#prepare_hardmock_controlObject



128
129
130
131
132
133
134
135
# File 'lib/hardmock.rb', line 128

def prepare_hardmock_control
  if @main_mock_control.nil?
    @main_mock_control = MockControl.new
    $main_mock_control = @main_mock_control
  else
    raise "@main_mock_control is already setup for this test!"
  end
end

#reset_stubsObject



164
165
166
# File 'lib/hardmock.rb', line 164

def reset_stubs
  Hardmock.restore_all_replaced_methods
end

#verify_mocks(force = true) ⇒ Object Also known as: verify_hardmocks

Ensures that all expectations have been met. If not, VerifyException is raised.

You normally won’t need to call this yourself. Within Test::Unit::TestCase, this will be done automatically at teardown time.

  • force – if false, and a VerifyError or ExpectationError has already occurred, this method will not raise. This is to help you suppress repeated errors when if you’re calling #verify_mocks in the teardown method of your test suite. BE WARNED - only use this if you’re sure you aren’t obscuring useful information. Eg, if your code handles exceptions internally, and an ExpectationError gets gobbled up by your rescue block, the cause of failure for your test may be hidden from you. For this reason, #verify_mocks defaults to force=true as of Hardmock 1.0.1



145
146
147
148
149
150
151
152
153
# File 'lib/hardmock.rb', line 145

def verify_mocks(force=true)
  return unless @main_mock_control
  return if @main_mock_control.disappointed? and !force
  @main_mock_control.verify
ensure
  @main_mock_control.clear_expectations if @main_mock_control
  $main_mock_control = nil
  reset_stubs
end