validate_unique_of()。scoped_to()导致RecordNotFound错误

时间:2019-07-11 17:11:56

标签: ruby-on-rails-4 rspec

我最近将我的应用程序从Rails 3升级到Rails 4,现在正在尝试运行单元测试。但是我在两个使用validate_uniqueness_of().scoped_to().

的地方遇到一个奇怪的错误

这是其中一种情况下的完全错误:

1) Distribution should require case sensitive unique value for collaborator_id scoped to variant_id
   Failure/Error: it { should validate_uniqueness_of(:collaborator_id).scoped_to(:variant_id) }
   ActiveRecord::RecordNotFound:
     Couldn't find Distribution with 'id'=first
   # ./spec/models/distribution_spec.rb:18:in `block (2 levels) in <top (required)>'

测试文件/spec/models/distribution_spec.rb包含:

subject { distribution }

let(:distribution) { create :distribution }

it { should validate_uniqueness_of(:collaborator_id).scoped_to(:variant_id) }

模型/app/models/distribution.rb包含:

validates_uniqueness_of :collaborator_id, :scope => :variant_id

我查看了类似错误的报告,但找不到任何'id'=first问题。

编辑:完整追溯

 # ./vendor/ruby/2.1.0/gems/activerecord-4.2.11.1/lib/active_record/relation/finder_methods.rb:324:in `raise_record_not_found_exception!'
 # ./vendor/ruby/2.1.0/gems/activerecord-4.2.11.1/lib/active_record/relation/finder_methods.rb:444:in `find_one'
 # ./vendor/ruby/2.1.0/gems/activerecord-4.2.11.1/lib/active_record/relation/finder_methods.rb:423:in `find_with_ids'
 # ./vendor/ruby/2.1.0/gems/activerecord-4.2.11.1/lib/active_record/relation/finder_methods.rb:71:in `find'
 # ./vendor/ruby/2.1.0/gems/activerecord-4.2.11.1/lib/active_record/querying.rb:3:in `find'
 # ./vendor/ruby/2.1.0/gems/composite_primary_keys-8.1.6/lib/composite_primary_keys/core.rb:25:in `find'
 # ./vendor/ruby/2.1.0/gems/shoulda-matchers-1.2.0/lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb:80:in `has_existing?'
 # ./vendor/ruby/2.1.0/gems/shoulda-matchers-1.2.0/lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb:71:in `matches?'
 # ./vendor/ruby/2.1.0/gems/rspec-expectations-3.3.1/lib/rspec/matchers/matcher_delegator.rb:14:in `method_missing'
 # ./vendor/ruby/2.1.0/gems/rspec-expectations-3.3.1/lib/rspec/expectations/handler.rb:50:in `block in handle_matcher'
 # ./vendor/ruby/2.1.0/gems/rspec-expectations-3.3.1/lib/rspec/expectations/handler.rb:27:in `with_matcher'
 # ./vendor/ruby/2.1.0/gems/rspec-expectations-3.3.1/lib/rspec/expectations/handler.rb:48:in `handle_matcher'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/memoized_helpers.rb:81:in `should'
 # ./spec/models/distribution_spec.rb:18:in `block (2 levels) in <top (required)>'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:206:in `instance_exec'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:206:in `block in run'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:430:in `block in with_around_and_singleton_context_hooks'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:388:in `block in with_around_example_hooks'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/hooks.rb:478:in `block in run'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/hooks.rb:618:in `block in run_around_example_hooks_for'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:273:in `call'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:273:in `call'
 # ./vendor/ruby/2.1.0/gems/rspec-rails-3.3.3/lib/rspec/rails/adapters.rb:127:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:378:in `instance_exec'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:378:in `instance_exec'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/hooks.rb:389:in `execute_with'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/hooks.rb:620:in `block (2 levels) in run_around_example_hooks_for'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:273:in `call'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:273:in `call'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/hooks.rb:621:in `run_around_example_hooks_for'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/hooks.rb:478:in `run'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:388:in `with_around_example_hooks'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:430:in `with_around_and_singleton_context_hooks'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/example.rb:203:in `run'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/example_group.rb:559:in `block in run_examples'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/example_group.rb:555:in `map'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/example_group.rb:555:in `run_examples'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/example_group.rb:521:in `run'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:115:in `block (3 levels) in run_specs'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:115:in `map'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:115:in `block (2 levels) in run_specs'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1627:in `with_suite_hooks'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:114:in `block in run_specs'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/reporter.rb:77:in `report'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:113:in `run_specs'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:89:in `run'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:73:in `run'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:41:in `invoke'
 # ./vendor/ruby/2.1.0/gems/rspec-core-3.3.2/exe/rspec:4:in `<top (required)>'
 # ./vendor/ruby/2.1.0/bin/rspec:23:in `load'
 # ./vendor/ruby/2.1.0/bin/rspec:23:in `<main>'

1 个答案:

答案 0 :(得分:0)

解决方案是升级shoulda-matchers Ruby gem。