在我的 ApplicationController
,我已经设置。
protect_from_forgery with: :exception
在我的 config/environments/test.rb
我已经设置了。
config.action_controller.allow_forgery_protection = false
我的理解是,这应该是防止真实性标记检查。但当我运行测试时,我的非GET
请求的结果。
Minitest::UnexpectedError: ActionController::InvalidAuthenticityToken
如果我评论出来 protect_from_forgery with: :exception
,我不再得到 InvalidAuthenticityToken
异常。
我怎么会得到 InvalidAuthenticityToken
如果 allow_forgery_protection
设置为 false
?
更新。 我知道我可以在运行测试时通过使用 protect_from_forgery with: :exception unless Rails.env.test?
. 我想问的是,为什么我试图在我的测试环境配置中通过禁用它 allow_forgery_protection = false
不起作用。
我花了点时间才弄明白,因为我也有类似的问题。
看起来好像是由于某种原因,config没有被尊重,但是在你的test_helper.rb中加入这个应该会有帮助。
ApplicationController.allow_forgery_protection = false