Joseph Wilk

Joseph Wilk

Things with code, creativity and computation.

Rspec-rails Mock_model Helper for the RR Test Double Framework

Rspec-rails is a rails plugin which brings the Rspec Ruby Behaviour Driven Development framework to rails along with some rails specific helpers. One of these hugely useful helper functions is:

mock_model(model_class, options_and_stubs = {})

This creates a mock object with the common methods stubbed out. It also allows you to specify other methods you want to stub.

You can use Rspec with a number of mocking frameworks (Rspec 1.1.5: mocha, flexmock & RR). Unfortunately the Rspec-rails mock_model helper is not compatible with the RR test double framework. It throws this error message:

undefined method `to_sym' for {:count=>0}:Hash

as it uses a stub function:

:errors => stub("errors", :count => 0)

which does not match RR’s stub method.

I’ve written a patch which will allow mock_model to work with RR. Note: It’s still a work in progress but I am using it successfully in a number of projects.

You can get the latest code from GitHub: git clone git://

[viewcode] src=../projects/ruby/rr/mocks_rr_extensions.rb geshi=ruby showsyntax=display[/viewcode]