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.
有人可以提供建議,如何在Ruby on Rails中測試AJAX?例如:我點擊按鈕,查詢轉到TestController #new,在那個方法中我們有respond_with(@test)和new.js.erb,其中是html表單。好吧,我可以點擊這個鏈接/按鈕,xhr查詢將工作,但我沒有得到任何響應,我需要測試。
Can some body help with this, please? I'm using Rspec.
有人可以幫忙解決這個問題嗎?我正在使用Rspec。
6
You should use Capybara and Selenium. Look at this great Railscast.
你應該使用Capybara和Selenium。看看這個偉大的Railscast。
6
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:
在Rails 4.2中,對於send ajax request,使用鍵“xhr:true”設置可選的hash參數來調用get,post,patch,put或delete方法。例如:
get users_path + '?page=2', xhr: true
and then use assert_select for verify some HTML elements on updated page.
然后使用assert_select驗證更新頁面上的一些HTML元素。
1
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(在水豚/硒的幫助下)測試您的應用程序流量。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/02/06/99aab82d4d4ee1438797b6a220638382.html。