如何在rspec rails请求测试中发布缺少authenticity_token的POST?

时间:2015-03-09 16:38:15

标签: ruby-on-rails rspec csrf authenticity-token

我正在模拟来自外部服务的请求,该服务不具有真实性令牌。如果缺少skip_before_action :verify_authenticity_token,我希望测试失败。

如何从Rspec请求规范执行此操作?

目前我正在使用post,如下所示,但很高兴接受。

post endpoint, my_json_string, {'CONTENT_TYPE' => "application/json"}

1 个答案:

答案 0 :(得分:34)

CSRF保护disabled in test environment。尝试启用它:

before do
  ActionController::Base.allow_forgery_protection = true
end

after do
  ActionController::Base.allow_forgery_protection = false
end