Mockr - easy Mock Objects for Ruby
Mockr is a pure Ruby library to support the Mock Objects approach to unit testing. Several other Mock Object libraries exist for Ruby. Mockr has two main distinguishing features:
- Support for the distinction between mocking and stubbing
- A constraint-based mechanism for matching call parameters
History and status
Mockr was begun in March 2005 and is based loosely on Java's JMock library. In fact, Mockr was originally called RMock, but it turned out the name was already used for a Java library.
It was presented at EuRuKo (European Ruby Conference) in October 2005 and is currently being polished for a public release.
Example
require 'test/unit' require 'mockr' ## Collaborates with a LaserGrid and a PoliceStationUplink class BurglarAlarm def initialize(laser_grid, police_link) @laser_grid = laser_grid @police_link = police_link end def check begin @police_link.incident("Grid breached") unless @laser_grid.intact? rescue @police_link.warning("Grid down") end end end class BurglarAlarmTest < Test::Unit::TestCase include Mockr::TestCaseHelpers def setup @laser_grid, @police_link = new_mock, new_mock @alarm = BurglarAlarm.new(@laser_grid.proxy, @police_link.proxy) end def test_police_station_not_contacted_if_grid_okay @laser_grid.expects.intact?.as { true } @alarm.check end def test_police_station_is_contacted_if_grid_not_okay @laser_grid.expects.intact?.as { false } @police_link.expects.incident("Grid breached") @alarm.check end def test_police_station_warned_if_grid_down @laser_grid.expects.intact?.as { raise IOError.new("comms down") } @police_link.expects.warning(/down/) # A loose parameter constraint @alarm.check end end
Getting it
Official releases are available in Gem form via Rubyforge, and can be installed with a simple gem install mockr.
To get the latest version, use git to access the master repository or download a snapshot tarball. See: http://git.sanityinc.com/?p=mockr.git;a=summary
