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’ve multiple return values specified, the stubbed method returns one at a time:

 it ​"stubs with multiple return values"​ ​do
  task = Task.new
  allow(task).to receive(​:size​).and_return(1, 2)
  assert_equal(1, task.size)
  assert_equal(2, task.size)
  assert_equal(2, task.size)
 end

The return values of the stubbed method walk through the values passed to and_return. Note that the values ...

Get Rails 5 Test Prescriptions now with O’Reilly online learning.

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