flexmock-0.6.3.rdoc

Path: doc/releases/flexmock-0.6.3.rdoc
Last Update: Wed Feb 09 10:47:13 +0000 2011

FlexMock 0.6.3 Released

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

New in 0.6.3

  • Added the flexmodel() method for better support when mocking ActiveRecord objects. flexmodel(YourModelClass) will return a pure mock object that responds to some basic ActiveRecord methods with reasonable defaults. The flexmodel() method is optional and you need to require ‘flexmock/activerecord’ to active it.
  • The flexmock() method now always returns a combination domain object / mock object. This means the object return can handle domain methods and mock-specific methods (such as should_receive and mock_teardown).
  • A side effect of always returning a domain/mock object is that partial mocks are now enhanced with about 5 or 6 extra methods. Since partial mocks are real objects with just a few methods mocked, there is a (small) potential for a method name conflict. FlexMock now supports a safe-mode for partial mocks if this is an issue in particular case (see the RDoc README file for more details).
  • Fixed a small bug where attempting to mock a method that the partial mock claims to respond to, but doesn‘t actually have defined would cause an error. This tended to happen on active record objects where attributes are dynamically handled.

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]