Mock expectations after exercising code in Rspec

David Santoro
Carwow Product, Design & Engineering
1 min readMar 14, 2016

This is going to be a very small post, I want to share a nice pattern that I’ve learned today. I’d like to thank Thoughtbot. Check out their article on the topic here: https://robots.thoughtbot.com/a-closer-look-at-test-spies

I needed to test that a certain object received a method with one of the parameters being the id of the object created during the test. In pseudocode this is what I wanted to test

Obviously that doesn’t work. And we can’t simply setup the expectation before exercising our code, because we don’t have the id of the Todo: it has not been created yet.

But the solution is pretty straightforward. We use “allow” on our mock service followed by an expectation that use the method “have_received” instead of “receive” and voilà, we have a lovely test for our interaction.

Enjoy!

Interested in making an Impact? Join the carwow-team!
Feeling social? Connect with us on Twitter and LinkedIn :-)

--

--