# ANN: FlexMock 0.6.0 Released

**URL:** <https://www.ruby-forum.com/t/ann-flexmock-0-6-0-released/92101>\
**Category:** Ruby\
**Created:** [April 16, 2007, 4:34am UTC](https://www.ruby-forum.com/t/ann-flexmock-0-6-0-released/92101 "2007-04-16T04:34:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimweirich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce73a5/32.png) [@jimweirich](https://www.ruby-forum.com/u/jimweirich)\
**Post date:** [April 16, 2007, 4:34am UTC](https://www.ruby-forum.com/t/ann-flexmock-0-6-0-released/92101/1 "2007-04-16T04:34:11Z")

</div>

= 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  
FlexMock::TestCase).

- Integration with RSpec (version 0.9.0 or later of RSpec is required).

- The +flexmock+ method will now create both regular mocks and partial  
mocks.

- Quick and simple mocks my now be created using an expectation hash.  
For  
example:

- The +should\_receive+ method will now allow multiple methods (with the  
same  
constraints) be defined in a single call. For example, the following  
declares that both :read and :write need to be called at least one  
time each  
on the mock object.

- +should\_recieve+ now will allow expectation hashes as arguments. This  
is  
similar to the list of methods, but allows each defined method to have  
its  
own return value.

- In addition to using a block for defining constrains, constraints may  
now be  
applied directly to the return value of +new\_instances+. Combined with  
the  
expectation hashes supported by +should\_receive+, simple mocking  
scenarios  
have become much more succinct. For example:

: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  
older  
versions of RCov. The FlexMock Rakefile now includes a RCov task (and  
we  
have 100% code coverage).

== 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 [http://flexmock.rubyforge.org](http://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:: [http://rubyforge.org/project/showfiles.php?group\_id=170](http://rubyforge.org/project/showfiles.php?group_id=170)

You will find documentation at: [http://flexmock.rubyforge.org](http://flexmock.rubyforge.org).

– Jim W.
