= FlexMock 0.6.0 Released
FlexMock is a flexible mocking library for use in unit testing and
behavior specification in Ruby. Version 0.6.0 introduces a number of
API enhancements to make testing with mocks even easier than before.
== New in 0.6.0
Better integration with Test::Unit (no need to explicitly include
Integration with RSpec (version 0.9.0 or later of RSpec is required).
The +flexmock+ method will now create both regular mocks and partial
flexmock() # => a full mock
flexmock(person) # => a partial mock based on person
(+flexstub+ is still included for backwards compatibility).
Quick and simple mocks my now be created using an expectation hash.
flexmock(:foo => 10, :bar => “Hello”)
will create a mock with two methods, :foo and :bar,defined. :foo will
return 10 when invoked, and :bar will return “Hello”.
The +should_receive+ method will now allow multiple methods (with the
constraints) be defined in a single call. For example, the following
declares that both :read and :write need to be called at least one
on the mock object.
+should_recieve+ now will allow expectation hashes as arguments. This
similar to the list of methods, but allows each defined method to have
own return value.
flexmock.should_receive(:name => “John”, :age => 32)
In addition to using a block for defining constrains, constraints may
applied directly to the return value of +new_instances+. Combined with
expectation hashes supported by +should_receive+, simple mocking
have become much more succinct. For example:
flexmock(Person).new_instances.should_receive(:name => "John",
:age => 32)
Improved implementation, allowing for more flexible use and greater
consistency between full mock and partial mocks.
Version 0.6.0 also includes a fix for an incompatibility with some
versions of RCov. The FlexMock Rakefile now includes a RCov task (and
have 100% code coverage).
== What is FlexMock?
FlexMock is a flexible framework for creating mock object for testing.
running unit tests, it is often desirable to use isolate the objects
tested from the “real world” by having them interact with simplified
objects. Sometimes these test objects simply return values when called,
times they verify that certain methods were called with particular
in a particular order.
FlexMock makes creating these test objects easy.
Easy integration with both Test::Unit and RSpec. Mocks created with
flexmock method are automatically verified at the end of the test or
A fluent interface that allows mock behavior to be specified very
A “record mode” where an existing implementation can record its
interaction with a mock for later validation against a new
Easy mocking of individual methods in existing, non-mock objects.
The ability to cause classes to instantiate test instances (instead of
instances) for the duration of a test.
Suppose you had a Dog object that wagged a tail when it was happy.
Something like this:
@tail = a_tail
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:
class TestDog < Test::Unit::TestCase
tail = flexmock(“tail”)
dog = Dog.new(tail)
FlexMock will automatically verify that the mocked tail object received
message +wag+ exactly one time. If it doesn’t, the test will not pass.
See the FlexMock documentation at http://flexmock.rubyforge.org for
specifying arguments and return values on mocked methods, as well as a
technique for mocking tail objects when the Dog class creates the tail
You can make sure you have the latest version with a quick RubyGems
gem install flexmock (you may need root/admin privileges)
Otherwise, you can get it from the more traditional places:
You will find documentation at: http://flexmock.rubyforge.org.
– Jim W.