Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would propose DRYing it up by redefining the test subject (and using <a href="http://blog.dynamicprogrammer.com/2010/09/15/learning-about-the-stabby-lambda-syntax-in-ruby.html">stabby lambdas</a> for fun):</p> <pre><code>describe "recipe creation" do subject { -&gt; { click_button submit } } it { should change(Recipe, :count).by(1) } it { should change(Ingredient, :count).by(1) } end </code></pre> <p><strong>Update</strong>: Although it may look less DRY, these days I would probably still keep on using the <code>expect</code> syntax, since it's <a href="http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax">recommended</a> and I'm generally moving away from <code>should</code>, but perhaps make some minor changes for spec readability:</p> <pre><code>describe "recipe creation" do let(:creating_a_recipe) { -&gt; { click_button submit } } it "changes the Recipe count" do expect(creating_a_recipe).to change(Recipe, :count).by(1) end it "changes the Ingredient count" do expect(creating_a_recipe).to change(Ingredient, :count).by(1) end end </code></pre> <p><strong>Note</strong>: you may see in the <a href="http://www.relishapp.com/rspec/rspec-expectations/v/3-3/docs/built-in-matchers/change-matcher">RSpec documentation for the <code>change</code> matcher</a> that <code>expect</code> uses curly brackets. This is, of course, correct, but the reason that standard parenthesis work in this example is that the code that changes mutable state (contained in <code>creating_a_recipe</code>) is in a lambda that gets called when passed into <code>expect</code> as a parameter.</p> <p>Regardless, in this case either <code>expect(creating_a_recipe)</code> or <code>expect { creating_a_recipe }</code> can be used successfully, and whichever one you use is up to personal preference.</p>
 

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