gpt4 book ai didi

ruby-on-rails - 覆盖特定集成测试的 action_controller.allow_forgery_protection

转载 作者:行者123 更新时间:2023-11-28 19:42:47 24 4
gpt4 key购买 nike

我有一个在我的基本应用程序 Controller 中使用 protect_from_forgery 的 rails3 应用程序。我正在使用 ActionDispatch::IntegrationTest 并希望确保在某些集成测试期间存在真实性 token 。

我不希望执行帖子的每个功能测试都必须传递 authenticity_token,所以我的 test.rb 文件指定:

  config.action_controller.allow_forgery_protection    = false

正如 Rails 文档所建议的那样。

但是,对于集成测试,我希望确保我的表单能够正确发送真实性 token 。如果不在 config/environments/test.rb

中全局更改设置,我找不到任何方法来执行此操作

如果我所有的表单都是用 form_for 生成的,我会相信 rails 会处理这个,但我使用 ExtJS 并且有许多 ExtJS 表单需要手动指定,所以我确实应该测试管道是否全部正常工作。

最佳答案

您可以简单地更改集成测试设置中的值:

require 'test_helper'

class MyCrunchyIntegrationTest < ActionController::IntegrationTest
fixtures :all

def setup
ActionController::Base.allow_forgery_protection = true
end

def teardown
ActionController::Base.allow_forgery_protection = false
end

test "how awesome my application is" do
# ...
end
end

关于ruby-on-rails - 覆盖特定集成测试的 action_controller.allow_forgery_protection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5917939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com