Rails 系统测试 w/ Capybara RackTest 引发 ActiveSupport::MessageVerifier::InvalidSignature

发布于 2025-01-12 02:32:33 字数 1955 浏览 3 评论 0 原文

我有一个典型的 Rails 模型表单,带有允许多个附件的文件附件选择器。它在开发中工作正常,但在系统测试期间,会引发 ActiveSupport::MessageVerifier::InvalidSignature 异常。

  • Rails 7.0.2.2
  • capybara 3.36.0rack
  • -test 1.1.0

模型 has_many_attached :photos

该表单使用 form_withmultipart: true

HTML 源代码看起来是正确的。

在开发中,手动使用带有 0 或任何文件附件的表单可以按预期工作。

在我的系统测试中,我使用 rack_test 驱动程序。

test "creating a quote request" do
  visit new_quote_request_path
  fill_in "First name", with: 'FAKE FIRST'
  # ...
  click_on "Submit"
  assert_text "Success"
end

在控制器中,我的规范参数允许方法如下所示:

def quote_request_params
  params.require(:quote_request).permit(:first_name, :last_name, :email,
    :phone_number, :shipping, :promo_code, :description, :item_type_id, :brand_id,
    photos: [])
end

我的控制器 create 方法是典型的...

def create
  @quote_request = QuoteRequest.new(quote_request_params)
  respond_to do |format|
  # ...

在系统测试中,调用 QuoteRequest.new(quote_request_params)引发 ActiveSupport::MessageVerifier::InvalidSignature 异常。

设置断点后,我可以看到 quote_request_params 看起来像:

#<ActionController::Parameters {"first_name"=>"FAKE FIRST",
"last_name"=>"FAKE LAST", "email"=>"[email protected]", 
"phone_number"=>"5415555555", "shipping"=>"1", "promo_code"=>"", 
"description"=>"Fake quote request description.", 
"item_type_id"=>"980190962", "brand_id"=>"980190962",
"photos"=>[
  "",
  "#<Capybara::RackTest::Form::NilUploadedFile:0x000000010dae35b8>"
]} permitted: true>

水豚似乎正在执行其为多部分表单附加“nil 文件”的默认行为。

为什么测试会引发 ActiveSupport::MessageVerifier::InvalidSignature 异常?

I have a typical Rails model form with a file attachment selector allowing multiple attachments. It works fine in development, but during a system test, raises an ActiveSupport::MessageVerifier::InvalidSignature exception.

  • Rails 7.0.2.2
  • capybara 3.36.0
  • rack-test 1.1.0

The model has_many_attached :photos.

The form is using form_with and multipart: true.

The HTML source looks correct.

In development, manually using the form with 0 or any file attachments works as expected.

In my system test, I am using the rack_test driver.

test "creating a quote request" do
  visit new_quote_request_path
  fill_in "First name", with: 'FAKE FIRST'
  # ...
  click_on "Submit"
  assert_text "Success"
end

In the controller, my canonical param-permitting method looks like:

def quote_request_params
  params.require(:quote_request).permit(:first_name, :last_name, :email,
    :phone_number, :shipping, :promo_code, :description, :item_type_id, :brand_id,
    photos: [])
end

My controller create method is typical...

def create
  @quote_request = QuoteRequest.new(quote_request_params)
  respond_to do |format|
  # ...

In the system test, the call of QuoteRequest.new(quote_request_params) raises an ActiveSupport::MessageVerifier::InvalidSignature exception.

With a breakpoint in place, I can see that the quote_request_params looks like:

#<ActionController::Parameters {"first_name"=>"FAKE FIRST",
"last_name"=>"FAKE LAST", "email"=>"[email protected]", 
"phone_number"=>"5415555555", "shipping"=>"1", "promo_code"=>"", 
"description"=>"Fake quote request description.", 
"item_type_id"=>"980190962", "brand_id"=>"980190962",
"photos"=>[
  "",
  "#<Capybara::RackTest::Form::NilUploadedFile:0x000000010dae35b8>"
]} permitted: true>

And it seems Capybara is doing its default behavior of attaching a 'nil file' for the multipart form.

Why is the test raising an ActiveSupport::MessageVerifier::InvalidSignature exception?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好菇凉咱不稀罕他 2025-01-19 02:32:33

这是 Rails 7 和机架测试的问题。可以暂时通过在config/environments/test.rb中添加以下内容来解决:

config.active_storage.multiple_file_field_include_hidden = false

参考机架测试问题了解更多详细信息。

This is an issue with Rails 7 and rack-test. It can temporarily be solved by putting the following in config/environments/test.rb:

config.active_storage.multiple_file_field_include_hidden = false

Refer to the rack-test issue for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文