Module: Mocha::ObjectMethods

### Home Classes Methods

### In Files

- lib/mocha/inspect.rb
- lib/mocha/object_methods.rb
- lib/mocha/parameter_matchers/object.rb

### Methods

- #expects
- #method_exists?
- #mocha
- #mocha_inspect
- #reset_mocha
- #stubba_method
- #stubba_object
- #stubs
- #to_matcher
- #unstub

### Class/Module Index

Quicksearch

- Mocha
- Mocha::API
- Mocha::AnyInstanceMethod
- Mocha::AnyInstanceReceiver
- Mocha::ArgumentIterator
- Mocha::ArrayMethods
- Mocha::BacktraceFilter
- Mocha::Cardinality
- Mocha::Central
- Mocha::ChangeStateSideEffect
- Mocha::ClassMethod
- Mocha::ClassMethods
- Mocha::ClassMethods::AnyInstance
- Mocha::Configuration
- Mocha::DateMethods
- Mocha::Debug
- Mocha::DefaultName
- Mocha::DefaultReceiver
- Mocha::Deprecation
- Mocha::Detection
- Mocha::Detection::MiniTest
- Mocha::Detection::TestUnit
- Mocha::ExceptionRaiser
- Mocha::Expectation
- Mocha::ExpectationError
- Mocha::ExpectationErrorFactory
- Mocha::ExpectationList
- Mocha::HashMethods
- Mocha::Hooks
- Mocha::ImpersonatingAnyInstanceName
- Mocha::ImpersonatingName
- Mocha::InStateOrderingConstraint
- Mocha::InstanceMethod
- Mocha::Integration
- Mocha::Integration::AssertionCounter
- Mocha::Integration::MiniTest
- Mocha::Integration::MiniTest::Adapter
- Mocha::Integration::MiniTest::Nothing
- Mocha::Integration::MiniTest::Version13
- Mocha::Integration::MiniTest::Version13::RunMethodPatch
- Mocha::Integration::MiniTest::Version140
- Mocha::Integration::MiniTest::Version140::RunMethodPatch
- Mocha::Integration::MiniTest::Version141
- Mocha::Integration::MiniTest::Version141::RunMethodPatch
- Mocha::Integration::MiniTest::Version142To172
- Mocha::Integration::MiniTest::Version142To172::RunMethodPatch
- Mocha::Integration::MiniTest::Version200
- Mocha::Integration::MiniTest::Version200::RunMethodPatch
- Mocha::Integration::MiniTest::Version201To222
- Mocha::Integration::MiniTest::Version201To222::RunMethodPatch
- Mocha::Integration::MiniTest::Version2110To2111
- Mocha::Integration::MiniTest::Version2110To2111::RunMethodPatch
- Mocha::Integration::MiniTest::Version2112To320
- Mocha::Integration::MiniTest::Version2112To320::RunMethodPatch
- Mocha::Integration::MiniTest::Version230To2101
- Mocha::Integration::MiniTest::Version230To2101::RunMethodPatch
- Mocha::Integration::MonkeyPatcher
- Mocha::Integration::TestUnit
- Mocha::Integration::TestUnit::Adapter
- Mocha::Integration::TestUnit::GemVersion200
- Mocha::Integration::TestUnit::GemVersion200::RunMethodPatch
- Mocha::Integration::TestUnit::GemVersion201To202
- Mocha::Integration::TestUnit::GemVersion201To202::RunMethodPatch
- Mocha::Integration::TestUnit::GemVersion203To220
- Mocha::Integration::TestUnit::GemVersion203To220::RunMethodPatch
- Mocha::Integration::TestUnit::GemVersion230To250
- Mocha::Integration::TestUnit::GemVersion230To250::RunMethodPatch
- Mocha::Integration::TestUnit::Nothing
- Mocha::Integration::TestUnit::RubyVersion185AndBelow
- Mocha::Integration::TestUnit::RubyVersion185AndBelow::RunMethodPatch
- Mocha::Integration::TestUnit::RubyVersion186AndAbove
- Mocha::Integration::TestUnit::RubyVersion186AndAbove::RunMethodPatch
- Mocha::Logger
- Mocha::MethodMatcher
- Mocha::Mock
- Mocha::Mockery
- Mocha::ModuleMethod
- Mocha::ModuleMethods
- Mocha::MultipleYields
- Mocha::Name
- Mocha::NoYields
- Mocha::ObjectMethods
- Mocha::ObjectReceiver
- Mocha::ParameterMatchers
- Mocha::ParameterMatchers::AllOf
- Mocha::ParameterMatchers::AnyOf
- Mocha::ParameterMatchers::AnyParameters
- Mocha::ParameterMatchers::Anything
- Mocha::ParameterMatchers::Base
- Mocha::ParameterMatchers::Equals
- Mocha::ParameterMatchers::HasEntries
- Mocha::ParameterMatchers::HasEntry
- Mocha::ParameterMatchers::HasKey
- Mocha::ParameterMatchers::HasValue
- Mocha::ParameterMatchers::Includes
- Mocha::ParameterMatchers::InstanceOf
- Mocha::ParameterMatchers::IsA
- Mocha::ParameterMatchers::KindOf
- Mocha::ParameterMatchers::Not
- Mocha::ParameterMatchers::Optionally
- Mocha::ParameterMatchers::QueryStringMatches
- Mocha::ParameterMatchers::RegexpMatches
- Mocha::ParameterMatchers::RespondsWith
- Mocha::ParameterMatchers::YamlEquivalent
- Mocha::ParametersMatcher
- Mocha::PrettyParameters
- Mocha::ReturnValues
- Mocha::Sequence
- Mocha::Sequence::InSequenceOrderingConstraint
- Mocha::SingleReturnValue
- Mocha::SingleYield
- Mocha::StateMachine
- Mocha::StateMachine::State
- Mocha::StateMachine::StatePredicate
- Mocha::StringMethods
- Mocha::StubbingError
- Mocha::Thrower
- Mocha::TimeMethods
- Mocha::UnexpectedInvocation
- Mocha::YieldParameters
- Array
- Date
- Hash
- Object
- String
- Time

No matching classes.

# Mocha::ObjectMethods

Methods added to all objects to allow mocking and stubbing on real (i.e. non-mock) objects.

Both {expects} and {stubs} return an {Expectation} which can be further modified by methods on {Expectation}.

### Public Instance Methods

expects(expected_methods_vs_return_values) click to toggle source

Adds an expectation that the specified method must be called exactly once with any parameters.

The original implementation of the method is replaced during the test and then restored at the end of the test. The temporary replacement method has the same visibility as the original method.

@param [Symbol,String] method_name name of expected method @param [Hash] expected_methods_vs_return_values expected method name symbols as keys and corresponding return values as values - these expectations are setup as if {expects} were called multiple times.

@overload def expects(method_name) @overload def expects(expected_methods_vs_return_values) @return [Expectation] last-built expectation which can be further modified by methods on {Expectation}. @raise [StubbingError] if attempting to stub method which is not allowed.

@example Setting up an expectation on a non-mock object.

    product = Product.new
    product.expects(:save).returns(true)
    assert_equal true, product.save

@example Setting up multiple expectations on a non-mock object.

    product = Product.new
    product.expects(:valid? => true, :save => true)

    # exactly equivalent to

    product = Product.new
    product.expects(:valid?).returns(true)
    product.expects(:save).returns(true)

@see Mock#expects

    # File lib/mocha/object_methods.rb, line 64
    def expects(expected_methods_vs_return_values)
      if expected_methods_vs_return_values.to_s =~ /the[^a-z]*spanish[^a-z]*inquisition/
        raise ExpectationErrorFactory.build('NOBODY EXPECTS THE SPANISH INQUISITION!')
      end
      if frozen?
        raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}", caller)
      end
      expectation = nil
      mockery = Mocha::Mockery.instance
      iterator = ArgumentIterator.new(expected_methods_vs_return_values)
      iterator.each { |*args|
        method_name = args.shift
        mockery.on_stubbing(self, method_name)
        method = stubba_method.new(stubba_object, method_name)
        mockery.stubba.stub(method)
        expectation = mocha.expects(method_name, caller)
        expectation.returns(args.shift) if args.length > 0
      }
      expectation
    end

method_exists?(method, include_public_methods = true) click to toggle source

@private

    # File lib/mocha/object_methods.rb, line 163
    def method_exists?(method, include_public_methods = true)
      if include_public_methods
        return true if public_methods(include_superclass_methods = true).include?(method)
        return true if respond_to?(method.to_sym)
      end
      return true if protected_methods(include_superclass_methods = true).include?(method)
      return true if private_methods(include_superclass_methods = true).include?(method)
      return false
    end

mocha() click to toggle source

@private

    # File lib/mocha/object_methods.rb, line 17
    def mocha
      @mocha ||= Mocha::Mockery.instance.mock_impersonating(self)
    end

mocha_inspect() click to toggle source

    # File lib/mocha/inspect.rb, line 6
    def mocha_inspect
      address = self.__id__ * 2
      address += 0x100000000 if address < 0
      inspect =~ /#</ ? "#<#{self.class}:0x#{'%x' % address}>" : inspect
    end

reset_mocha() click to toggle source

@private

    # File lib/mocha/object_methods.rb, line 22
    def reset_mocha
      @mocha = nil
    end

stubba_method() click to toggle source

@private

    # File lib/mocha/object_methods.rb, line 27
    def stubba_method
      Mocha::InstanceMethod
    end

stubba_object() click to toggle source

@private

    # File lib/mocha/object_methods.rb, line 32
    def stubba_object
      self
    end

stubs(stubbed_methods_vs_return_values) click to toggle source

Adds an expectation that the specified method may be called any number of times with any parameters.

The original implementation of the method is replaced during the test and then restored at the end of the test. The temporary replacement method has the same visibility as the original method.

@param [Symbol,String] method_name name of stubbed method @param [Hash] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {stubs} were called multiple times.

@overload def stubs(method_name) @overload def stubs(stubbed_methods_vs_return_values) @return [Expectation] last-built expectation which can be further modified by methods on {Expectation}. @raise [StubbingError] if attempting to stub method which is not allowed.

@example Setting up a stubbed methods on a non-mock object.

    product = Product.new
    product.stubs(:save).returns(true)
    assert_equal true, product.save

@example Setting up multiple stubbed methods on a non-mock object.

    product = Product.new
    product.stubs(:valid? => true, :save => true)

    # exactly equivalent to

    product = Product.new
    product.stubs(:valid?).returns(true)
    product.stubs(:save).returns(true)

@see Mock#stubs

    # File lib/mocha/object_methods.rb, line 113
    def stubs(stubbed_methods_vs_return_values)
      if frozen?
        raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}", caller)
      end
      expectation = nil
      mockery = Mocha::Mockery.instance
      iterator = ArgumentIterator.new(stubbed_methods_vs_return_values)
      iterator.each { |*args|
        method_name = args.shift
        mockery.on_stubbing(self, method_name)
        method = stubba_method.new(stubba_object, method_name)
        mockery.stubba.stub(method)
        expectation = mocha.stubs(method_name, caller)
        expectation.returns(args.shift) if args.length > 0
      }
      expectation
    end

to_matcher() click to toggle source

@private

    # File lib/mocha/parameter_matchers/object.rb, line 7
    def to_matcher
      Mocha::ParameterMatchers::Equals.new(self)
    end

unstub(*method_names) click to toggle source

Removes the specified stubbed methods (added by calls to {expects} or {stubs}) and all expectations associated with them.

Restores the original behaviour of the methods before they were stubbed. This is normally done automatically at the end of each test, but in some circumstances you may want to do it before the end of the test.

WARNING: If you {unstub} a method which still has unsatisfied expectations, you may be removing the only way those expectations can be satisfied. Use {unstub} with care.

@param [Array<Symbol>] method_names names of methods to unstub.

@example Stubbing and unstubbing a method on a real (non-mock) object.

    multiplier = Multiplier.new
    multiplier.double(2) # => 4
    multiplier.stubs(:double).raises # new behaviour defined
    multiplier.double(2) # => raises exception
    multiplier.unstub(:double) # original behaviour restored
    multiplier.double(2) # => 4

@example Unstubbing multiple methods on a real (non-mock) object.

    multiplier.unstub(:double, :triple)

    # exactly equivalent to

    multiplier.unstub(:double)
    multiplier.unstub(:triple)

    # File lib/mocha/object_methods.rb, line 154
    def unstub(*method_names)
      mockery = Mocha::Mockery.instance
      method_names.each do |method_name|
        method = stubba_method.new(stubba_object, method_name)
        mockery.stubba.unstub(method)
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.