FlexMock 0.4.1 Released

FlexMock is a flexible mocking library for use in Ruby's Test::Unit test framework. Version 0.4.0 enhances FlexMock with the ability to stub and mock methods in existing objects.

New in 0.4.1

Coming fast on the heels of 0.4.0, version 0.4.1 fixes a problem with mocking methods that have the same name as methods defined in Kernel.

What is FlexMock?

FlexMock is a flexible Ruby mocking library that works with Ruby's Test::Unit framework to create easy to use mocks.



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
  def happy

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'

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

  def test_dog_wags_tail_when_happy
    tail = flexmock("tail")
    dog = Dog.new(tail)

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 onestepback.org/software/flexmock 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.


FlexMock is distributed with Rails, or 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:



You will find documentation at: onestepback.org/software/flexmock/

-- Jim Weirich