title: Clean Rspec matcher for change(Model, :count).by(1)
body: <p>I'm working hard trying to keep my spec files as clean as possible. Using 'shoulda' gem and writing customized matchers that follow the same pattern.</p>
  
  <p>My question is about creating a custom matcher that would wrap <code>expect{ post :create ... }.to change(Model, :count).by(1)</code> and could be used in the same example groups with other 'shoulda' matchers. Details bellow:</p>
  
  <p><strong>Custom matcher (simplified)</strong></p>
  
  <pre><code>RSpec::Matchers.define :create_a_new do |model|
    match do |dummy|
      ::RSpec::Expectations::ExpectationTarget.new(subject).to change(model, :count).by(1)
    end
  end
  </code></pre>
  
  <p><strong>Working example</strong></p>
  
  <pre><code>describe 'POST create:' do
    describe '(valid params)' do
      subject { -&gt; { post :create, model: agency_attributes } }
      it { should create_a_new(Agency) }
    end
  end
  </code></pre>
  
  <p>This work OK as long as I use a <code>subject</code> lambda and my matcher is the only one in the example group.</p>
  
  <p><strong>Failing examples</strong></p>
  
  <p><em>Failing example 1</em></p>
  
  <p>Adding more examples in the same group makes the other matcher fail because <code>subject</code> is now a lambda instead of an instance of the Controller.</p>
  
  <pre><code>describe 'POST create:' do
    describe '(valid params)' do
      subject { -&gt; { post :create, model: agency_attributes } }
      it { should create_a_new(Agency) }
      it { should redirect_to(Agency.last) }
    end
  end
  </code></pre>
  
  <p><em>Failing example 2</em></p>
  
  <p>The 'shoulda' matcher expect me to define a <code>before</code> block, but this become incompatible with my custom matcher</p>
  
  <pre><code>describe 'POST create:' do
    describe '(valid params)' do
      before { post :create, agency: agency_attributes }
      it { should create_a_new(Agency) }
      it { should redirect_to(Agency.last) }
    end
  end
  </code></pre>
  
  <p><strong>Expected result</strong></p>
  
  <p>I am looking for a way to write my custom matcher that would fit in the same example group as other matchers, meaning my custom matcher should use the <code>before</code> block to execute the controller action, the "failing example #2" above is the way I would like to write my specs. Is it possible?</p>
  
  <p>Thanks for reading</p>