简体   繁体   English

Rails rspec 应该使用匹配器验证不区分大小写的项目失败的唯一性吗?

[英]Rails rspec shoulda matcher validating uniqueness of case insensitive item failing?

I'm trying to be a good rails developer and write tests as I go.我正在努力成为一名优秀的 Rails 开发人员,并随时编写测试。 I've run into something I'm unclear on and am looking for advice.我遇到了一些我不清楚的事情,正在寻求建议。 I have a model that has a unique case insensitive attribute.我有一个具有独特的不区分大小写属性的模型。 The test is failing however.然而,测试失败了。 Whats the correct way to test this?什么是测试这个的正确方法? What am I doing wrong?我做错了什么?

class Tenant < ApplicationRecord
  validates :name, presence: true
  validates :name, uniqueness: { case_sensitive: false }
end

RSpec.describe Tenant, type: :model do
  it { should validate_presence_of :name }
  it { should validate_uniqueness_of(:name).case_insensitive }
end

It seems like it's trying to set the id as nil even though we have another validation that requires the presence.即使我们有另一个需要存在的验证,它似乎也试图将 id 设置为nil But why is it doing that while testing name?但是为什么在测试名称时这样做呢? I'm confused.我很困惑。

Test shows the following result;测试显示以下结果;

Failures:

 1) Tenant Validates Uniqueness of should validate that :name is case-insensitively unique
 Failure/Error: self.id = self.id.downcase

 NoMethodError:
   undefined method `downcase' for nil:NilClass
 # ./app/models/tenant.rb:17:in `block in <class:Tenant>'
 # /Users/a/.rvm/gems/ruby-2.5.0/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/validator.rb:96:in `perform_validation'
 # /Users/a/.rvm/gems/ruby-2.5.0/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/validator.rb:89:in `validation_result'
 # /Users/a/.rvm/gems/ruby-2.5.0/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/validator.rb:85:in `validation_error_messages'
 # /Users/a/.rvm/gems/ruby-2.5.0/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/validator.rb:64:in `messages'
 # /Users/a/.rvm/gems/ruby-2.5.0/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/validator.rb:25:in `has_messages?'
 # /Users/a/.rvm/gems/ruby-2.5.0/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/validator.rb:55:in `messages_match?'
 # /Users/a/.rvm/gems/ruby-2.5.0/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/validator.rb:21:in `call'
 # /Users/a/.rvm/gems/ruby-2.5.0/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setters_and_validators.rb:38:in `matches?'
 # /Users/a/.rvm/gems/ruby-2.5.0/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setters_and_validators.rb:24:in `each'
 # /Users/a/.rvm/gems/ruby-2.5.0/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setters_and_validators.rb:24:in `detect'
 # /Users/a/.rvm/gems/ruby-2.5.0/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/allow_value_matcher/attribute_setters_and_validators.rb:24:in `first_passing'
 # /Users/a/.rvm/gems/ruby-2.5.0/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/allow_value_matcher.rb:533:in `public_send'
 # /Users/a/.rvm/gems/ruby-2.5.0/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/allow_value_matcher.rb:533:in `run'
 # /Users/a/.rvm/gems/ruby-2.5.0/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/allow_value_matcher.rb:400:in `does_not_match?'
 # /Users/a/.rvm/gems/ruby-2.5.0/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/disallow_value_matcher.rb:32:in `matches?'
 # /Users/a/.rvm/gems/ruby-2.5.0/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/validation_matcher.rb:155:in `run_allow_or_disallow_matcher'
 # /Users/a/.rvm/gems/ruby-2.5.0/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_model/validation_matcher.rb:93:in `disallows_value_of'
 # /Users/a/.rvm/gems/ruby-2.5.0/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb:606:in `validate_two_records_with_same_non_blank_value_cannot_coexist?'
 # /Users/a/.rvm/gems/ruby-2.5.0/gems/shoulda-matchers-3.1.2/lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb:330:in `matches?'
 # ./spec/models/tenant_spec.rb:49:in `block (3 levels) in <top (required)>'

 Finished in 0.09495 seconds (files took 1.89 seconds to load)
 2 examples, 1 failure

 Failed examples:

 rspec ./spec/models/tenant_spec.rb:49 # Tenant Validates Uniqueness of should validate that :name is case-insensitively unique

Note: this is likely something dumb and obvious that I'm just missing.注意:这可能是我遗漏的愚蠢和明显的东西。 Your help/advice is appreciated.感谢您的帮助/建议。

Versions: Ruby 2.50 :: Rails 5.14 :: Rspec 3.7 :: Shoulda-matcher 3.12版本:Ruby 2.50 :: Rails 5.14 :: Rspec 3.7 :: Shoulda-matcher 3.12

As nattfodd mentions :正如nattfodd 提到的

It seems you have some before/after hooks defined.似乎您定义了一些之前/之后的挂钩。 Please show more code of Tenant model class.请显示Tenant模型类的更多代码。

It was a beforehook that was the issue.这是一个预先考虑的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM