[RSpec] How to Send Ajax Requests in Controller Tests

Tadashi Shigeoka ·  Wed, February 13, 2013

In RSpec, to send Ajax requests in controller tests, you can use xhr.

■ Ajax GET

xhr :get, :index

■ Ajax POST

xhr :post, :create, id: 1

You can use it like this. Simple!

describe "Ajax GET 'index'" do
  it "returns http success" do
    xhr :get, :index
    response.should be_success
  end
end

・[Reference]:ruby on rails - How do you test an AJAX request with RSpec/RoR? - Stack Overflow

That’s all from the Gemba.