I’ve recently started using Mocha. It was pretty easy to integrate into my unit tests and sped them up somewhat.
I added one line to my test_helper.rb:
require 'mocha'
I separated my should blocks into contexts by the mocks I needed to use and created mocks in my setup blocks:
@plugin_value_mock = mock('PluginName::ClassValue')
@plugin_value_mock.stubs(:value => 5.00)
It wasn’t nearly as simple for my functional tests. Before the expects, they looked like this:
context "GET on :index" do
setup do
get_as @user, :index
end
should_respond_with :success
should_assign_to :objects, :title
should_render_template 'index.html.erb'
end # index
I couldn’t just put the expects statements inside the should blocks like I did in the unit tests! Some quick research found the before_should method, which is a beautiful solution to my problem:
context "GET on :index" do
setup do
get_as @user, :index
end
before_should "get all values" do
PluginName.expects(:get_values).returns(@standard_value_mock).once
PluginName.expects(:get_ClassValue).returns([@plugin_value_mock]).at_least_once
end
should_respond_with :success
should_assign_to :objects, :title,
should_render_template 'index.html.erb'
end # index

Parisian Roundabout
A garden roundabout near the Champs Élysées in Paris – October 2010