Class FlexMock
In: lib/flexmock.rb
Parent: Object

FlexMock is a flexible mock object suitable for using with Ruby‘s Test::Unit unit test framework. FlexMock has a simple interface that‘s easy to remember, and leaves the hard stuff to all those other mock object implementations.

Basic Usage:

  m = FlexMock.new("name")
  m.mock_handle(:meth) { |args| assert_stuff }

Simplified Usage:

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

With Test::Unit Integration:

  class TestSomething < Test::Unit::TestCase
    include FlexMock::TestCase

    def test_something
      m = flexmock("name")
      m.should_receive(:hi).and_return("Hello")
      m.hi
    end
  end

Note: When using Test::Unit integeration, don‘t forget to include FlexMock::TestCase. Also, if you override teardown, make sure you call super.

Methods

Included Modules

Test::Unit::Assertions Test::Unit::Assertions

Classes and Modules

Module FlexMock::ArgumentTypes
Module FlexMock::TestCase
Class FlexMock::AnyMatcher
Class FlexMock::AtLeastCountValidator
Class FlexMock::AtMostCountValidator
Class FlexMock::BadInterceptionError
Class FlexMock::ClassProxy
Class FlexMock::CountValidator
Class FlexMock::EqualMatcher
Class FlexMock::ExactCountValidator
Class FlexMock::Expectation
Class FlexMock::ExpectationDirector
Class FlexMock::Factory
Class FlexMock::Interception
Class FlexMock::ProcMatcher
Class FlexMock::Recorder
Class FlexMock::StubProxy

Constants

ANY = AnyMatcher.new

External Aliases

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

Attributes

mock_current_order  [RW] 
mock_groups  [R] 
mock_name  [R] 

Public Class methods

Check will assert the block returns true. If it doesn‘t, an assertion failure is triggered with the given message.

Class method to format a method name and argument list as a nice looking string.

Create a FlexMock object with the given name. The name is used in error messages.

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.

Public Instance methods

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

Handle missing methods by attempting to look up a handler.

Allocation a new order number from the mock.

Return a factory object that returns this mock. This is useful in Class Interception.

Handle all messages denoted by sym by calling the given block and passing any parameters to the block. If we know exactly how many calls are to be made to a particular method, we may check that by passing in the number of expected calls as a second paramter.

mock_ignore_missing()

Teardown and infrastructure setup for this mock.

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

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

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

Ignore all undefined (missing) method calls.

Declare that the mock object should receive a message with the given name. 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.

[Validate]