Class: FlexMock::MockContainerHelper

### Home Classes Methods

### In Files

- lib/flexmock/mock_container.rb

### Parent

Object

### Methods

- #add_model_methods
- #make_partial_proxy
- #next_id

### Included Modules

- FlexMock::ArgumentTypes

### Files

- CHANGES
- GoogleExample.rdoc
- rspec_examples_spec.rdoc
- test_unit_examples_test.rdoc
- index.rdoc
- flexmock-0.4.0.rdoc
- flexmock-0.4.1.rdoc
- flexmock-0.4.2.rdoc
- flexmock-0.4.3.rdoc
- flexmock-0.5.0.rdoc
- flexmock-0.5.1.rdoc
- flexmock-0.6.0.rdoc
- flexmock-0.6.1.rdoc
- flexmock-0.6.2.rdoc
- flexmock-0.6.3.rdoc
- flexmock-0.6.4.rdoc
- flexmock-0.7.0.rdoc
- flexmock-0.7.1.rdoc
- flexmock-0.8.0.rdoc
- flexmock-0.8.2.rdoc
- flexmock-0.8.3.rdoc
- flexmock-0.8.4.rdoc
- flexmock-0.8.5.rdoc
- flexmock-0.9.0.rdoc
- flexmock-1.0.0.rdoc
- flexmock-1.0.3.rdoc
- flexmock-1.0.4.rdoc
- flexmock-1.1.0.rdoc
- flexmock-1.2.0.rdoc
- flexmock-1.3.0.rdoc
- flexmock-1.3.1.rdoc

### Class/Module Index

Quicksearch

- FlexMock
- FlexMock::AnyMatcher
- FlexMock::ArgumentMatching
- FlexMock::ArgumentTypes
- FlexMock::AtLeastCountValidator
- FlexMock::AtMostCountValidator
- FlexMock::CompositeExpectation
- FlexMock::CountValidator
- FlexMock::DefaultFrameworkAdapter
- FlexMock::DefaultFrameworkAdapter::AssertionFailedError
- FlexMock::DuckMatcher
- FlexMock::EqualMatcher
- FlexMock::ExactCountValidator
- FlexMock::Expectation
- FlexMock::ExpectationDirector
- FlexMock::ExpectationRecorder
- FlexMock::ExplicitNeeded
- FlexMock::GenericTestCase
- FlexMock::HashMatcher
- FlexMock::MockContainer
- FlexMock::MockContainerHelper
- FlexMock::MockError
- FlexMock::OptionalProcMatcher
- FlexMock::Ordering
- FlexMock::PartialMockProxy
- FlexMock::ProcMatcher
- FlexMock::RSpecFrameworkAdapter
- FlexMock::RSpecFrameworkAdapter::AssertionFailedError
- FlexMock::RSpecMatchers
- FlexMock::RSpecMatchers::HaveReceived
- FlexMock::Recorder
- FlexMock::SpyDescribers
- FlexMock::TestCase
- FlexMock::TestUnitAssertions
- FlexMock::TestUnitFrameworkAdapter
- FlexMock::Undefined
- FlexMock::UsageError
- FlexMock::UseContainer
- FlexMock::Version
- MiniTest
- MiniTest::Unit
- MiniTest::Unit::TestCase
- Test
- Test::Unit
- Test::Unit::TestCase
- Class
- Module
- Object
- Symbol

No matching classes.

# 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.