Max Lukin
Max Lukin

Reputation: 135

How to test AJAX with Ruby on Rails?

Can somebody give advice, how to test AJAX in Ruby on Rails? For example: i click to button, query going to TestController#new, and in that method we have respond_with(@test) and new.js.erb, where is html form. Ok, i can click_to this link/button, xhr query will work, but i don't get any responses, which i need to test.

Can some body help with this, please? I'm using Rspec.

Upvotes: 3

Views: 4448

Answers (3)

sadnix
sadnix

Reputation: 113

In Rails 4.2 , for send ajax request, set optional hash argument with key "xhr: true" to call of get, post, patch, put, or delete method. For example:

get users_path + '?page=2', xhr: true

and then use assert_select for verify some HTML elements on updated page.

Upvotes: 9

piotrb
piotrb

Reputation: 364

Depending on what your Ajax request has .. it may actually be best to test it on multiple levels.

Test your controller's response with a controller test.

Test your JS correctly dealing with this response with a pure JS testing framework like jasmine.

Test your flow through the application with something like cucumber, or an integration rspec (with the help of capybara/selenium).

Upvotes: 2

apneadiving
apneadiving

Reputation: 115541

You should use Capybara and Selenium. Look at this great Railscast.

Upvotes: 6

Related Questions