Rails 系统测试 w/ Capybara RackTest 引发 ActiveSupport::MessageVerifier::InvalidSignature
我有一个典型的 Rails 模型表单,带有允许多个附件的文件附件选择器。它在开发中工作正常,但在系统测试期间,会引发 ActiveSupport::MessageVerifier::InvalidSignature
异常。
- Rails 7.0.2.2
- capybara 3.36.0rack
- -test 1.1.0
模型 has_many_attached :photos
。
该表单使用 form_with
和 multipart: 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
异常?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 Rails 7 和机架测试的问题。可以暂时通过在
config/environments/test.rb
中添加以下内容来解决:参考机架测试问题了解更多详细信息。
This is an issue with Rails 7 and rack-test. It can temporarily be solved by putting the following in
config/environments/test.rb
:Refer to the rack-test issue for more details.