Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a less intrusive alternative to Rspec's `should_receive`?
    primarykey
    data
    text
    <p>In writing Rspec tests, I'm often frustrated with <code>should_receive</code>. I'd like to know if there's a less intrusive alternative.</p> <p>For example:</p> <pre><code>describe "making a cake" do it "should use some other methods" do @baker.should_receive(:make_batter) @baker.make_cake end end </code></pre> <p>The call to <code>should_receive</code> is a good description, but it breaks my code, because <code>should_receive</code> works by masking the original method, and <code>make_cake</code> can't proceed unless <code>make_batter</code> actually returns some batter. So I change it to this:</p> <pre><code>@baker.should_receive(:make_batter).and_return(@batter) </code></pre> <p>This is ugly because:</p> <ul> <li>It <strong>looks</strong> like I'm testing that <code>make_batter</code> correctly returns <code>@batter</code>, but I'm <strong>actually</strong> forcing the fake version of <code>make_batter</code> to return that.</li> <li>It forces me to separately set up <code>@batter</code></li> <li>If <code>make_batter</code> has any important side effects (which could be a code smell, I suppose) I have to make those happen, too.</li> </ul> <p>I wish that <code>should_receive(:make_batter)</code> would verify the method call and <strong>pass it on to the original method</strong>. If I wanted to stub its behavior for better isolation testing, I would do so explicitly: <code>@baker.stub(:make_batter).and_return(@batter)</code>.</p> <p>Is there a way to do something like <code>should_receive</code> without preventing the original method call? Is my problem a symptom of bad design?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload