O'Reilly logo

Rails 4 Test Prescriptions by Noel Rappin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

More Expectation Annotations

RSpec allows a number of different annotations to the expectation part of declaring a test double. You can specify more complex return values or a variety of arguments to the stubbed method.

Stubbing Return Values

A couple of advanced usages of returns might be valuable now and again. If you have multiple return values specified, the stubbed method returns them one at a time:

mocks/01/gatherer/spec/models/project_spec.rb
 
it ​"stubs with multiple returns"​ ​do
 
project = Project.new
 
allow(project).to receive(:user_count).and_return(1, 2)
 
assert_equal(1, project.user_count)
 
assert_equal(2, project.user_count)
 
assert_equal(2, project.user_count)
 
end

The return values of the stubbed method walk through the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required