Class: FlexMock::MockContainerHelper

Inherits:
Object
  • Object
show all
Includes:
ArgumentTypes
Defined in:
lib/gems/flexmock-0.8.3/lib/flexmock/mock_container.rb

Overview

################################################################# Helper methods for mock containers. MockContainer is a module that is designed to be mixed into other classes, particularly testing framework test cases. Since we don’t want to pollute the method namespace of the class that mixes in MockContainer, a number of MockContainer methods were moved into ContainerHelper to to isoloate the names.

Instance Method Summary collapse

Methods included from ArgumentTypes

#any, #eq, #on

Instance Method Details

#add_model_methods(mock, model_class, id) ⇒ Object

Automatically add mocks for some common methods in ActiveRecord models.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/mock_container.rb', line 208

def add_model_methods(mock, model_class, id)
  container = mock.flexmock_container
  mock.should_receive(
    :id => id,
    :to_params => id.to_s,
    :new_record? => false,
    :class => model_class,
    :errors => container.flexmock("errors", :count => 0))
  # HACK: Ruby 1.9 needs the following lambda so that model_class
  # is correctly bound below.
  lambda { }
  mock.should_receive(:is_a?).with(any).and_return { |other|
    other == model_class
  }
  mock.should_receive(:instance_of?).with(any).and_return { |other|
    other == model_class
  }
  mock.should_receive(:kind_of?).with(any).and_return { |other|
    model_class.ancestors.include?(other)
  }
end

#make_partial_proxy(container, obj, name, safe_mode) ⇒ Object

Create a PartialMockProxy for the given object. Use name as the name of the mock object.



232
233
234
235
236
237
238
239
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/mock_container.rb', line 232

def make_partial_proxy(container, obj, name, safe_mode)
  name ||= "flexmock(#{obj.class.to_s})"
  obj.instance_eval {
    mock = FlexMock.new(name, container)
    @flexmock_proxy ||= PartialMockProxy.new(obj, mock, safe_mode)
  }
  obj.instance_variable_get("@flexmock_proxy")
end

#next_idObject

Return the next id for mocked models.



177
178
179
180
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/mock_container.rb', line 177

def next_id
  @id_counter ||= 10000
  @id_counter += 1
end

#parse_should_args(mock, args, &block) ⇒ Object

:call-seq:

parse_should_args(args) { |symbol| ... }

This method provides common handling for the various should_receive argument lists. It sorts out the differences between symbols, arrays and hashes, and identifies the method names specified by each. As each method name is identified, create a mock expectation for it using the supplied block.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/mock_container.rb', line 190

def parse_should_args(mock, args, &block)  # :nodoc:
  result = CompositeExpectation.new
  args.each do |arg|
    case arg
    when Hash
      arg.each do |k,v|
        exp = build_demeter_chain(mock, k, &block).and_return(v)
        result.add(exp)
      end
    when Symbol, String
      result.add(build_demeter_chain(mock, arg, &block))
    end
  end
  result
end