当测试环境中禁用防伪保护时,测试如何抛出InvalidAuthenticityToken?

问题描述 投票:1回答:1

在我的 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 不起作用。

ruby-on-rails ruby minitest
1个回答
1
投票

我花了点时间才弄明白,因为我也有类似的问题。

看起来好像是由于某种原因,config没有被尊重,但是在你的test_helper.rb中加入这个应该会有帮助。

ApplicationController.allow_forgery_protection = false
© www.soinside.com 2019 - 2024. All rights reserved.