ruby-on-rails 在rspec测试中的proc行上出现Ruby 3.x参数错误

1mrurvl1  于 2023-03-13  发布在  Ruby
关注(0)|答案(1)|浏览(150)

我正在尝试将一个Ruby 2.7应用程序转换为Ruby 3.0.4。此应用程序运行Rails 6.1.7和rspec 5.1.2。我有一个CommonIssue模型,它与Issue有许多关系。我正在获取

#<ArgumentError: wrong number of arguments (given 2, expected 0..1)>

从以下行

proc { CommonIssue.first.issues << Issue.first }
  .should raise_error("Validation failed: Issue has already been taken")

下面的形式也给出了相同的错误

expect { CommonIssue.first.issues << Issue.first }
  .to raise_error("Validation failed: Issue has already been taken")

我没有看到任何两个参数。上面的第一行代码抛出了这个错误吗?我相信“应该”行是可以的,它是上面的一行。
堆栈跟踪:

Failure/Error:
   proc { CommonIssue.first.issues << Issue.first }
     .should raise_error("Validation failed: Issue has already been taken")
 
   expected Exception with "Validation failed: Issue has already been taken", got #<ArgumentError: wrong number of arguments (given 2, expected 0..1)> with backtrace:
     # /usr/local/bundle/gems/i18n-1.12.0/lib/i18n.rb:210:in `translate'
     # ./config/initializers/dynamic_form_monkey_patch.rb:55:in `block (2 levels) in full_messages'
     # ./config/initializers/dynamic_form_monkey_patch.rb:47:in `each'
     # ./config/initializers/dynamic_form_monkey_patch.rb:47:in `block in full_messages'
     # /usr/local/bundle/gems/activemodel-6.1.7/lib/active_model/errors.rb:237:in `each'
     # /usr/local/bundle/gems/activemodel-6.1.7/lib/active_model/errors.rb:237:in `each'
     # ./config/initializers/dynamic_form_monkey_patch.rb:34:in `full_messages'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/validations.rb:21:in `initialize'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/validations.rb:80:in `new'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/validations.rb:80:in `raise_validation_error'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/validations.rb:53:in `save!'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/transactions.rb:302:in `block in save!'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/transactions.rb:354:in `block in with_transaction_returning_status'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:318:in `transaction'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/transactions.rb:350:in `with_transaction_returning_status'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/transactions.rb:302:in `save!'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/suppressor.rb:48:in `save!'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/associations/has_many_through_association.rb:80:in `save_through_record'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/associations/has_many_through_association.rb:31:in `insert_record'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/associations/collection_association.rb:437:in `block (2 levels) in concat_records'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/associations/collection_association.rb:462:in `replace_on_target'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/associations/collection_association.rb:283:in `add_to_target'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/associations/collection_association.rb:435:in `block in concat_records'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/associations/collection_association.rb:433:in `each'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/associations/collection_association.rb:433:in `concat_records'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/associations/has_many_association.rb:130:in `concat_records'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/associations/has_many_through_association.rb:40:in `concat_records'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/associations/collection_association.rb:121:in `block in concat'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/associations/collection_association.rb:136:in `block in transaction'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:320:in `block in transaction'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/connection_adapters/abstract/transaction.rb:319:in `block in within_new_transaction'
     # /usr/local/bundle/gems/activesupport-6.1.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'
     # /usr/local/bundle/gems/activesupport-6.1.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
     # /usr/local/bundle/gems/activesupport-6.1.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
     # /usr/local/bundle/gems/activesupport-6.1.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
     # /usr/local/bundle/gems/activesupport-6.1.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/connection_adapters/abstract/transaction.rb:317:in `within_new_transaction'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:320:in `transaction'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/transactions.rb:209:in `transaction'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/associations/collection_association.rb:135:in `transaction'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/associations/collection_association.rb:121:in `concat'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/associations/has_many_through_association.rb:21:in `concat'
     # /usr/local/bundle/gems/activerecord-6.1.7/lib/active_record/associations/collection_proxy.rb:1027:in `<<'
     # ./spec/models/common_issue_spec.rb:46:in `block (3 levels) in <top (required)>'
     # ./spec/models/common_issue_spec.rb:46:in `block (2 levels) in <top (required)>'
     # ./spec/spec_helper.rb:76:in `block (3 levels) in <top (required)>'
     # /usr/local/bundle/gems/database_cleaner-core-2.0.1/lib/database_cleaner/strategy.rb:30:in `cleaning'
     # /usr/local/bundle/gems/database_cleaner-core-2.0.1/lib/database_cleaner/cleaners.rb:34:in `block (2 levels) in cleaning'
     # /usr/local/bundle/gems/database_cleaner-core-2.0.1/lib/database_cleaner/cleaners.rb:35:in `cleaning'
     # ./spec/spec_helper.rb:75:in `block (2 levels) in <top (required)>'
     # /usr/local/bundle/gems/webmock-3.18.1/lib/webmock/rspec.rb:37:in `block (2 levels) in <top (required)>'
 # ./spec/models/common_issue_spec.rb:46:in `block (2 levels) in <top (required)>'
 # ./spec/spec_helper.rb:76:in `block (3 levels) in <top (required)>'
 # /usr/local/bundle/gems/database_cleaner-core-2.0.1/lib/database_cleaner/strategy.rb:30:in `cleaning'
 # /usr/local/bundle/gems/database_cleaner-core-2.0.1/lib/database_cleaner/cleaners.rb:34:in `block (2 levels) in cleaning'
 # /usr/local/bundle/gems/database_cleaner-core-2.0.1/lib/database_cleaner/cleaners.rb:35:in `cleaning'
 # ./spec/spec_helper.rb:75:in `block (2 levels) in <top (required)>'
 # /usr/local/bundle/gems/webmock-3.18.1/lib/webmock/rspec.rb:37:in `block (2 levels) in <top (required)>'
ws51t4hk

ws51t4hk1#

事实证明,堆栈跟踪中的monkey补丁文件才是真实的的问题所在。特别是它使用了需要关键字参数的I18n翻译函数。我能够通过重写monkey补丁来修复这个问题,该补丁显然是为了基于这个信息通过I18n翻译运行来自模型的消息而设计的。[https://www.rubydoc.info/gems/dynamic_form/1.1.4/ActiveModel/Errors][1]
然而,重写这个补丁以消 debugging 误带来了另一个错误。

/usr/local/bundle/gems/i18n-1.12.0/lib/i18n.rb:57:in `config': stack level too deep (SystemStackError)

这不是我的决定,但我越是这样做,我就越想要求不要使用它,因为它的麻烦比它的价值。

相关问题