Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [ANN] FlexMock 0.4.0 Released

Reply
Thread Tools

[ANN] FlexMock 0.4.0 Released

 
 
Jim Weirich
Guest
Posts: n/a
 
      09-05-2006
= FlexMock 0.4.0 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 Features in 0.4.0

* You can now use the flexstub(object) method to mock or stub
individual methods in an existing object. The original definitions
of the methods are restored at the end of a test.

* The and_return (and its alias, returns) now accepts a list of
arguments and will return the values in the list one at a time for
each successive call to the mocked method.

* The flexmock() method now accepts an initialization block so that a
mock can be created and configured in one step without using a local
variable. This is really handy when mocking factory or creation
methods that in turn return a mock.

== What is FlexMock?

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

=== Features

* Easy integration with Test::Unit. Mocks created with the flexmock
method are automatically verified at the end of the test.

* 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.

=== 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'

class TestDog < Test::Unit::TestCase
include FlexMock::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://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.

== Availability

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:

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

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

-- Jim Weirich

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
flexmock not working in cygwin Feng Tien Ruby 2 05-07-2008 10:20 PM
How do you use flexmock or mocha to test user interaction? Feng Tien Ruby 1 11-12-2007 02:53 PM
ANN: FlexMock 0.6.0 Released Jim Weirich Ruby 0 04-16-2007 02:34 AM
problem with FlexMock::TestCase#flexstub in 0.4.0 John Wilger Ruby 0 09-05-2006 10:57 PM
Flexmock question Jon A. Lambert Ruby 2 10-01-2005 06:48 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57