DRY up Rspec with tap!
Posted on June 25, 2012
I’m a big fan of keeping Rspec DRY. Mostly, because it hurts my heart to have to write the same thing twice. It huuuurts. Here’s a great article on code reuse in Rspec. A scenario I struggle with DRYing up: I’d like to either stub! or call should_receive on a method and sometimes return a value. Previously, I would still have to write the stub! or should_receive twice — one with and_returns and one without.
Never fear. Ruby 1.9.3 here! And it comes with tap, which just fucking rocks. Here’s how I used it in a recent Rspec method: Is your world rocked? Of course it is. You’re welcome.