File: flexmock-0.7.0.rdoc

# flexmock-0.7.0.rdoc

Path: | doc/releases/flexmock-0.7.0.rdoc
Last Update: | Tue Aug 12 16:53:25 -0600 2008

# FlexMock 0.7.0 Released

FlexMock is a flexible mocking library for use in unit testing and behavior specification in Ruby. Version 0.7.0 introduces several enhancements.

## New in 0.7.0

- FlexMock now supports the ability to mock a chain of method calls automatically. For example:

        car = flexmock("car", "chassis.engine.piston.stroke" => :ok)
        assert_equal :ok, car.chassis.engine.piston.stroke

  will create a sequence of mocks so that the "chassis" call will return a mock that responds to "engine", which returns a mock that responds to "piston", which returns a mock that responds to "stroke". This facility makes mocking legacy code that violates the Law of Demeter a bit easier to deal with.

- Added the the `and_yield` constraint to FlexMock expectations. This allows the user to easily specify values passed to any block given to the mock method.
- Globally ordering of mocked calls is now optionally available. When a mock method is globally ordered, it must be called in the correct order with regard to all other globally ordered methods. Non-global ordering only requires that the method calls be ordered with regard to other methods on the same mock object.
- The output for mock.inspect was modified to be much more consise, so that test framework error messages do not overwhelm the output.
- In order to clean up the method namespace, a number of internally used methods were deprecated. All non-public methods that get added to mocks, partial mocks or test frameworks now begin with "flexmock_" (rather than "mock_"). The "mock_*" versions are still available, but will display deprecation warnings when used. The deprecated "mock_*" methods will be removed in a future version.
- Additionally, the ancient "mock_handle" method has been deprecated (prints a warning when used), and will be removed in a future version. Users are encouraged to use the newer "should_receive" method instead.

## New Features Added in 0.6.x

In case you missed them, here are a number of features that were added during the 0.6.x versions of FlexMock.

- ActiveRecord mocking support with flexmock(:model, ModelName).
- Better partial mock definitions, including a "safe-mode" that minimizes mock namespace pollution in the domain object.
- Support for `and_raise` constraint to ease the definition of mocks that raise exceptions.

## What is FlexMock?

FlexMock is a flexible framework for creating mock object for testing. When running unit tests, it is often desirable to use isolate the objects being tested from the "real world" by having them interact with simplified test objects. Sometimes these test objects simply return values when called, other times they verify that certain methods were called with particular arguments in a particular order.

FlexMock makes creating these test objects easy.

### Features

- Easy integration with both Test::Unit and RSpec. Mocks created with the flexmock method are automatically verified at the end of the test or example.
- A fluent interface that allows mock behavior to be specified very easily.
- A "record mode" where an existing implementation can record its interaction with a mock for later validation against a new implementation.
- Easy mocking of individual methods in existing, non-mock objects.
- The ability to cause classes to instantiate test instances (instead of real instances) for the duration of a test.

### Example

Suppose you had a Dog object that wagged a tail when it was happy. Something like this:

      class Dog
        def initialize(a_tail)
          @tail = a_tail
        end
        def happy
          @tail.wag
        end
      end

To test the `Dog` class without a real `Tail` object (perhaps because real `Tail` objects activate servos in some robotic equipment), you can do something like this:

require ‘test/unit’ require ‘flexmock/test_unit‘

      class TestDog < Test::Unit::TestCase
        def test_dog_wags_tail_when_happy
          tail = flexmock("tail")
          tail.should_receive(:wag).once
          dog = Dog.new(tail)
          dog.happy
        end
      end

FlexMock will automatically verify that the mocked tail object received the message `wag` exactly one time. If it doesn‘t, the test will not pass.

See the FlexMock documentation at flexmock.rubyforge.org for details on specifying arguments and return values on mocked methods, as well as a simple technique for mocking tail objects when the Dog class creates the tail objects directly.

## Availability

You can make sure you have the latest version with a quick RubyGems command:

      gem install flexmock    (you may need root/admin privileges)

Otherwise, you can get it from the more traditional places:

Download: | rubyforge.org/project/showfiles.php?group_id=170

You will find documentation at: flexmock.rubyforge.org.

[Validate]