更新到 Rails 4.1 后,我在新项目中遇到了一个关于 Cucumber 和 Capybara 的有趣问题。
在一个 View 中,我放置了一些缩略图。用户应该单击缩略图链接以接收有关他选择的人的更多信息。通过 AJAX 的魔力,信息会出现在缩略图下方。这是我在 View 中的做法:
<%= link_to( image_tag( ... ), "/controller/action.js&person=#{@person.nickname}", id: @person.thumb_id , remote: true) %
对于这种情况, Controller 遵循通常的程序
respond_to do format.js end
等
在浏览器中完美运行,我喜欢它。
但是,Cucumber 和 Capybara 就没那么顺利了。这是让我很头疼的 Capybara 系列:
When(/^I click on one of the portraits to display the person's stuff$/) do
click_link("jack_sparrow_THUMB") # @user.thumb_id
end
使用 Cucumber 运行场景,我收到上述语句的错误消息:
Security warning: an embedded <script> tag on another site requested protected
JavaScript. If you know what you're doing, go ahead and disable forgery protection
on this action to permit cross-origin JavaScript embedding.
(ActionController::InvalidCrossOriginRequest)
问题一定与此有关
http://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection.html
看看提供的 CROSS_ORIGIN_JAVASCRIPT_WARNING ... :(
我可以做些什么来让我的测试再次运行而无需降级到 rails < 4.1 或什至关闭一般的请求伪造保护?非常感谢您的帮助。
最佳答案
根据 Rails 指南中的“CSRF protection from remote tags”:
在测试的情况下,你也做客户端,从:
get :index, format: :js
收件人:
xhr :get, :index, format: :js
关于ruby-on-rails - cucumber + capybara 测试 : AJAX RequestForgeryProtection trouble since Rails 4. 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24983098/