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

Get Rails 4 Test Prescriptions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.