Class: FlexMock

### Home Classes Methods

### In Files

- lib/flexmock/argument_matchers.rb
- lib/flexmock/argument_matching.rb
- lib/flexmock/argument_types.rb
- lib/flexmock/call_record.rb
- lib/flexmock/call_validator.rb
- lib/flexmock/composite_expectation.rb
- lib/flexmock/core.rb
- lib/flexmock/core_class_methods.rb
- lib/flexmock/default_framework_adapter.rb
- lib/flexmock/deprecated_methods.rb
- lib/flexmock/errors.rb
- lib/flexmock/expectation.rb
- lib/flexmock/expectation_builder.rb
- lib/flexmock/expectation_director.rb
- lib/flexmock/expectation_recorder.rb
- lib/flexmock/explicit_needed.rb
- lib/flexmock/extensions/active_record_model.rb
- lib/flexmock/mock_builder.rb
- lib/flexmock/mock_container.rb
- lib/flexmock/ordering.rb
- lib/flexmock/partial_mock.rb
- lib/flexmock/recorder.rb
- lib/flexmock/rspec.rb
- lib/flexmock/rspec_spy_matcher.rb
- lib/flexmock/spy_describers.rb
- lib/flexmock/test_unit_assert_spy_called.rb
- lib/flexmock/test_unit_integration.rb
- lib/flexmock/test_unit_testcase_extensions.rb
- lib/flexmock/undefined.rb
- lib/flexmock/validators.rb
- lib/flexmock/version.rb

### Parent

Object

### Namespace

- MODULE FlexMock::ArgumentMatching
- MODULE FlexMock::ArgumentTypes
- MODULE FlexMock::Extensions
- MODULE FlexMock::GenericTestCase
- MODULE FlexMock::MockContainer
- MODULE FlexMock::Ordering
- MODULE FlexMock::RSpecMatchers
- MODULE FlexMock::SpyDescribers
- MODULE FlexMock::TestCase
- MODULE FlexMock::TestUnitAssertions
- MODULE FlexMock::Version
- CLASS FlexMock::AnyMatcher
- CLASS FlexMock::AtLeastCountValidator
- CLASS FlexMock::AtMostCountValidator
- CLASS FlexMock::CallValidator
- CLASS FlexMock::CompositeExpectation
- CLASS FlexMock::CountValidator
- CLASS FlexMock::DefaultFrameworkAdapter
- CLASS FlexMock::DuckMatcher
- CLASS FlexMock::EqualMatcher
- CLASS FlexMock::ExactCountValidator
- CLASS FlexMock::Expectation
- CLASS FlexMock::ExpectationBuilder
- CLASS FlexMock::ExpectationDirector
- CLASS FlexMock::ExpectationRecorder
- CLASS FlexMock::ExplicitNeeded
- CLASS FlexMock::ExtensionRegistry
- CLASS FlexMock::HashMatcher
- CLASS FlexMock::MockBuilder
- CLASS FlexMock::MockError
- CLASS FlexMock::OptionalProcMatcher
- CLASS FlexMock::PartialMockProxy
- CLASS FlexMock::ProcMatcher
- CLASS FlexMock::RSpecFrameworkAdapter
- CLASS FlexMock::Recorder
- CLASS FlexMock::TestUnitFrameworkAdapter
- CLASS FlexMock::Undefined
- CLASS FlexMock::UsageError
- CLASS FlexMock::UseContainer

### Methods

- ::format_args
- ::new
- ::undefined
- ::use
- #by_default
- #flexmock_based_on
- #flexmock_calls
- #flexmock_define_expectation
- #flexmock_invoke_original
- #flexmock_received?
- #flexmock_respond_to?
- #flexmock_teardown
- #flexmock_verify
- #inspect
- #method
- #method_missing
- #mock_ignore_missing
- #respond_to?
- #should_expect
- #should_ignore_missing
- #should_receive

### Included Modules

- FlexMock::Ordering

### 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::CallValidator
- FlexMock::CompositeExpectation
- FlexMock::CountValidator
- FlexMock::DefaultFrameworkAdapter
- FlexMock::DefaultFrameworkAdapter::AssertionFailedError
- FlexMock::DuckMatcher
- FlexMock::EqualMatcher
- FlexMock::ExactCountValidator
- FlexMock::Expectation
- FlexMock::ExpectationBuilder
- FlexMock::ExpectationDirector
- FlexMock::ExpectationRecorder
- FlexMock::ExplicitNeeded
- FlexMock::ExtensionRegistry
- FlexMock::Extensions
- FlexMock::Extensions::ActiveRecordModel
- FlexMock::GenericTestCase
- FlexMock::HashMatcher
- FlexMock::MockBuilder
- FlexMock::MockContainer
- 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

+

+

FlexMock is a flexible mock object framework for creating and using test doubles (mocks, stubs and spies).

Basic Usage:

    m = flexmock("name")
    m.should_receive(:upcase).with("stuff").
      and_return("STUFF")
    m.should_receive(:downcase).with(String).
      and_return { |s| s.downcase }.once

With Test::Unit Integration:

    class TestSomething < Test::Unit::TestCase
      def test_something
        m = flexmock("name")
        m.should_receive(:hi).and_return("Hello")
        m.hi
      end
    end

Note: Also, if you override `teardown`, make sure you call `super`.

Deprecated Methods

The following methods are no longer supported in FlexMock. Include this file for legacy applications.

    Permission is granted for use, copying, modification, distribution,
    and distribution of modified versions of this work as long as the
    above copyright notice is included.

+++

    Permission is granted for use, copying, modification, distribution,
    and distribution of modified versions of this work as long as the
    above copyright notice is included.

+++

    Permission is granted for use, copying, modification, distribution,
    and distribution of modified versions of this work as long as the
    above copyright notice is included.

+++

    Permission is granted for use, copying, modification, distribution,
    and distribution of modified versions of this work as long as the
    above copyright notice is included.

+++

### Constants

ANY
CALL_VALIDATOR
CONTAINER_HELPER
CallRecord
EXP_BUILDER
OPTIONAL_PROC_MATCHER
SpecModule
VERSION

### Attributes

framework_adapter[R]
partials_are_based[RW]
flexmock_container[RW]
flexmock_name[R]

### Public Class Methods

format_args(args) click to toggle source

Class method to format a list of args (the part between the parenthesis).

    # File lib/flexmock/core_class_methods.rb, line 59
    def format_args(args)
      if args
        args.collect { |a| a.inspect }.join(', ')
      else
        "*args"
      end
    end

new(name="unknown", container=nil) click to toggle source

Create a FlexMock object with the given name. The name is used in error messages. If no container is given, create a new, one-off container for this mock.

    # File lib/flexmock/core.rb, line 58
    def initialize(name="unknown", container=nil)
      @flexmock_name = name
      @expectations = Hash.new
      @ignore_missing = false
      @verified = false
      @calls = []
      @base_class = nil
      container = UseContainer.new if container.nil?
      container.flexmock_remember(self)
    end

undefined() click to toggle source

Undefined is normally available as FlexMock.undefined

    # File lib/flexmock/undefined.rb, line 47
    def self.undefined
      @undefined
    end

use(*names) click to toggle source

Class method to make sure that verify is called at the end of a test. One mock object will be created for each name given to the use method. The mocks will be passed to the block as arguments. If no names are given, then a single anonymous mock object will be created.

At the end of the use block, each mock object will be verified to make sure the proper number of calls have been made.

Usage:

    FlexMock.use("name") do |mock|    # Creates a mock named "name"
      mock.should_receive(:meth).
        returns(0).once
    end                               # mock is verified here

NOTE: If you include FlexMock::TestCase into your test case file, you can create mocks that will be automatically verified in the test teardown by using the `flexmock` method.

    # File lib/flexmock/core_class_methods.rb, line 39
    def use(*names)
      names = ["unknown"] if names.empty?
      container = UseContainer.new
      mocks = names.collect { |n| container.flexmock(n) }
      yield(*mocks)
    rescue Exception => _
      container.got_exception = true
      raise
    ensure
      container.flexmock_teardown
    end

### Public Instance Methods

by_default() click to toggle source

    # File lib/flexmock/core.rb, line 97
    def by_default
      @last_expectation.by_default
      self
    end

flexmock_based_on(base_class) click to toggle source

    # File lib/flexmock/core.rb, line 139
    def flexmock_based_on(base_class)
      @base_class = base_class
      should_receive(:class => base_class)
    end

flexmock_calls() click to toggle source

Return the list of calls made on this mock. Used in formatting error messages.

    # File lib/flexmock/core.rb, line 153
    def flexmock_calls
      @calls
    end

flexmock_define_expectation(location, *args) click to toggle source

Using `location`, define the expectations specified by `args`.

    # File lib/flexmock/core.rb, line 199
    def flexmock_define_expectation(location, *args)
      @last_expectation = EXP_BUILDER.parse_should_args(self, args) do |method_name|
        @expectations[method_name] ||= ExpectationDirector.new(method_name)
        result = Expectation.new(self, method_name, location)
        @expectations[method_name] << result
        override_existing_method(method_name) if flexmock_respond_to?(method_name)
        result = ExplicitNeeded.new(result, method_name, @base_class) if
          @base_class && ! @base_class.flexmock_defined?(method_name)
        result
      end
    end

flexmock_invoke_original(method_name, args) click to toggle source

Invocke the original non-mocked functionality for the given symbol.

    # File lib/flexmock/core.rb, line 159
    def flexmock_invoke_original(method_name, args)
      return FlexMock.undefined
    end

flexmock_received?(method_name, args, options={}) click to toggle source

True if the mock received the given method and arguments.

    # File lib/flexmock/core.rb, line 147
    def flexmock_received?(method_name, args, options={})
      CALL_VALIDATOR.received?(@calls, method_name, args, options)
    end

flexmock_respond_to?(sym, *args) click to toggle source

Save the original definition of respond_to? for use a bit later.

Alias for: respond_to?
flexmock_teardown() click to toggle source

Teardown and infrastructure setup for this mock.

    # File lib/flexmock/core.rb, line 87
    def flexmock_teardown
    end

flexmock_verify() click to toggle source

Verify that each method that had an explicit expected count was actually called that many times.

    # File lib/flexmock/core.rb, line 76
    def flexmock_verify
      return if @verified
      @verified = true
      flexmock_wrap do
        @expectations.each do |sym, handler|
          handler.flexmock_verify
        end
      end
    end

inspect() click to toggle source

Return the inspection string for a mock.

    # File lib/flexmock/core.rb, line 70
    def inspect
      "<FlexMock:#{flexmock_name}>"
    end

method(method_name) click to toggle source

Override the built-in `method` to include the mocked methods.

    # File lib/flexmock/core.rb, line 164
    def method(method_name)
      @expectations[method_name] || super
    rescue NameError => ex
      if @ignore_missing
        proc { FlexMock.undefined }
      else
        raise ex
      end
    end

method_missing(sym, *args, &block) click to toggle source

Handle missing methods by attempting to look up a handler.

    # File lib/flexmock/core.rb, line 103
    def method_missing(sym, *args, &block)
      enhanced_args = block_given? ? args + [block] : args
      call_record = CallRecord.new(sym, enhanced_args, block_given?)
      @calls << call_record
      flexmock_wrap do
        if handler = @expectations[sym]
          handler.call(enhanced_args, call_record)
        elsif @base_class && @base_class.flexmock_defined?(sym)
          FlexMock.undefined
        elsif @ignore_missing
          FlexMock.undefined
        else
          super(sym, *args, &block)
        end
      end
    end

mock_ignore_missing() click to toggle source
Alias for: should_ignore_missing
respond_to?(sym, *args) click to toggle source

Override the built-in respond_to? to include the mocked methods.

    # File lib/flexmock/core.rb, line 124
    def respond_to?(sym, *args)
      super || (@expectations[sym] ? true : @ignore_missing)
    end

Also aliased as: flexmock_respond_to?
should_expect() click to toggle source

Declare that the mock object should expect methods by providing a recorder for the methods and having the user invoke the expected methods in a block. Further expectations may be applied the result of the recording call.

Example Usage:

    mock.should_expect do |record|
      record.add(Integer, 4) { |a, b|
        a + b
      }.at_least.once

    # File lib/flexmock/core.rb, line 223
    def should_expect
      yield Recorder.new(self)
    end

should_ignore_missing() click to toggle source

Ignore all undefined (missing) method calls.

    # File lib/flexmock/core.rb, line 91
    def should_ignore_missing
      @ignore_missing = true
      self
    end

Also aliased as: mock_ignore_missing
should_receive(:method_name) click to toggle source
should_receive(:method1, method2, ...)
should_receive(:meth1 => result1, :meth2 => result2, ...)

Declare that the mock object should receive a message with the given name.

If more than one method name is given, then the mock object should expect to receive all the listed melthods. If a hash of method name/value pairs is given, then the each method will return the associated result. Any expectations applied to the result of `should_receive` will be applied to all the methods defined in the argument list.

An expectation object for the method name is returned as the result of this method. Further expectation constraints can be added by chaining to the result.

See Expectation for a list of declarators that can be used.

    # File lib/flexmock/core.rb, line 193
    def should_receive(*args)
      location = caller.first
      flexmock_define_expectation(location, *args)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.