简体   繁体   English

如何使用Ruby on Rails测试AJAX?

[英]How to test AJAX with Ruby on Rails?

Can somebody give advice, how to test AJAX in Ruby on Rails? 有人可以提供建议,如何在Ruby on Rails中测试AJAX? 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. 例如:我点击按钮,查询转到TestController #new,在那个方法中我们有respond_with(@test)和new.js.erb,其中是html表单。 Ok, i can click_to this link/button, xhr query will work, but i don't get any responses, which i need to test. 好吧,我可以点击这个链接/按钮,xhr查询将工作,但我没有得到任何响应,我需要测试。

Can some body help with this, please? 有人可以帮忙解决这个问题吗? I'm using Rspec. 我正在使用Rspec。

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. 在Rails 4.2中,对于send ajax request,使用键“xhr:true”设置可选的hash参数来调用get,post,patch,put或delete方法。 For example: 例如:

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

and then use assert_select for verify some HTML elements on updated page. 然后使用assert_select验证更新页面上的一些HTML元素。

You should use Capybara and Selenium. 你应该使用Capybara和Selenium。 Look at this great Railscast . 看看这个伟大的Railscast

Depending on what your Ajax request has .. it may actually be best to test it on multiple levels. 根据您的Ajax请求有什么......实际上最好在多个级别上进行测试。

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. 使用像jasmine这样的纯JS测试框架测试你的JS正确处理这个响应。

Test your flow through the application with something like cucumber, or an integration rspec (with the help of capybara/selenium). 使用类似黄瓜或整合rspec(在水豚/硒的帮助下)测试您的应用程序流量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM