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