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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.