You can leverage Jasmine’s ` andCallFake ` and ` andReturn ` to modify the return value every time the stub is called.
Drop the following function definitions into ` spec_helper.js ` and start stubbing!
How does it work?
Let’s take a look at the first function:
This simply creates the initial spy on the object and returns the result of our other function - ` returnConsecutive `.
The ` returnConsecutive ` function does two things:
returns the next value
modify what the stub returns
This is what it looks like with just returning the next value:
Now let’s take a look at where the chaining is happening:
If there are more than one value that still needs to be returned, we modify the stub to call ` returnConsecutive ` again to
return the next set of values.
If there is only a single value left to be returned, we set the stub to statically return the last value using
` andReturn `. This means the stub will keep returning the last value in the array after all of them have been returned.