title: How do I test ajax POST request with RSpec?
body: <p>I just want to test ajax request on controller spec. The product code is below. I'm using Devise for authentication.</p>
  
  <pre class="lang-rb prettyprint-override"><code>class NotesController &lt; ApplicationController
    def create
      if request.xhr?
        @note = Note.new(params[:note])
        if @note.save
          render json: { notice: "success" }
        end
      end
    end
  end
  </code></pre>
  
  <p>And spec is below.</p>
  
  <pre class="lang-rb prettyprint-override"><code>describe NotesController do
    before do
      user = FactoryGirl.create(:user)
      user.confirm!
      sign_in user
    end
  
    it "has a 200 status code" do
      xhr :post, :create, note: { title: "foo", body: "bar" }, format: :json
      response.code.should == "200"
    end
  end
  </code></pre>
  
  <p>I expect the response code to be 200, but it returns 401. I guess it must be because the request which rspec throws lacks authenticity_token or something. How can I stub it?</p>
  
  <p>Any help would be greatly appreciated.</p>