class FlexMock::MockContainerHelper

################################################################# 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.

Constants

METHOD_NAME_RE

Public Instance Methods

add_model_methods(mock, model_class, id, location) click to toggle source

Automatically add mocks for some common methods in ActiveRecord models.

# File lib/flexmock/mock_container.rb, line 238
def add_model_methods(mock, model_class, id, location)
  container = mock.flexmock_container

  mock_errors = container.flexmock("errors")
  mock_errors.flexmock_define_expectation(location, :count).and_return(0).by_default
  mock_errors.flexmock_define_expectation(location, :full_messages).and_return([]).by_default

  mock.flexmock_define_expectation(location, :id).and_return(id).by_default
  mock.flexmock_define_expectation(location, :to_params).and_return(id.to_s).by_default
  mock.flexmock_define_expectation(location, :new_record?).and_return(false).by_default
  mock.flexmock_define_expectation(location, :class).and_return(model_class).by_default
  mock.flexmock_define_expectation(location, :errors).and_return(mock_errors).by_default

  # HACK: Ruby 1.9 needs the following lambda so that model_class
  # is correctly bound below.
  lambda { }
  mock.flexmock_define_expectation(location, :is_a?).with(any).and_return { |other|
    other == model_class
  }.by_default
  mock.flexmock_define_expectation(location, :instance_of?).with(any).and_return { |other|
    other == model_class
  }.by_default
  mock.flexmock_define_expectation(location, :kind_of?).with(any).and_return { |other|
    model_class.ancestors.include?(other)
  }.by_default
end
make_partial_proxy(container, obj, name, safe_mode) click to toggle source

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

# File lib/flexmock/mock_container.rb, line 267
def make_partial_proxy(container, obj, name, safe_mode)
  name ||= "flexmock(#{obj.class.to_s})"
  if !obj.instance_variable_defined?("@flexmock_proxy") || obj.instance_variable_get("@flexmock_proxy").nil?
    mock = FlexMock.new(name, container)
    proxy = PartialMockProxy.new(obj, mock, safe_mode)
    obj.instance_variable_set("@flexmock_proxy", proxy)
  end
  obj.instance_variable_get("@flexmock_proxy")
end
next_id() click to toggle source

Return the next id for mocked models.

# File lib/flexmock/mock_container.rb, line 207
def next_id
  @id_counter ||= 10000
  @id_counter += 1
end