匹配器的范围唯一性测试应针对范围中不允许的nil值进行测试

时间:2019-02-15 19:54:34

标签: ruby-on-rails rspec rspec-rails shoulda

我的应该匹配者应该验证season_storage_id的唯一性,其范围是时代。这样做应该忽略大小写,因为我在验证之前就改写了输入。

#app/models/season_storage.rb

before_validation :sanitize_inputs
...   
validates :storage_id, uniqueness: { scope: :epoch }
validates :epoch, presence: true
validates :epoch, format: /\A20[0-9]{2}-(spring|autumn)\Z/
...
def sanitize_inputs
  storage_id.upcase!
end



#spec/models/season_storage.rb

it { is_expected.to validate_uniqueness_of(:storage_id).ignoring_case_sensitivity.scoped_to(:epoch) }

在运行此测试时,发生了意外的事情:

Failure/Error: it { is_expected.to validate_uniqueness_of(:storage_id).ignoring_case_sensitivity.scoped_to(:epoch) }              

       Expected SeasonStorage to validate that :storage_id is unique within the                                                        
       scope of :epoch, but this could not be proved.
         After taking the given SeasonStorage, setting its :storage_id to                                                              
         ‹"dummy value"›, and saving it as the existing record, then making a                                                          
         new SeasonStorage and setting its :storage_id to ‹"dummy value"› (read                                                        
         back as ‹"DUMMY VALUE"›) as well e its :epoch to a different value,                                                           
         ‹nil›, the matcher expected the new SeasonStorage to be invalid, but                                                          
         it was valid instead.

         As indicated in the message above, :storage_id seems to be changing                                                           
         certain values as they are set, and this could have something to do                                                           
         with why this test is failing. If you or something else has overridden                                                        
         the writer method for this attribute to normalize values by changing                                                          
         their case in any way (for instance, ensuring that the attribute is                                                           
         always downcased), then try adding `ignoring_case_sensitivity` onto                                                           
         the end of the uniqueness matcher. Otherwise, you may need to write                                                           
         the test yourself, or do something different altogether.

这是我不明白的那一行:

as well e its :epoch to a different value,                                                           
‹nil›, the matcher expected the new SeasonStorage to be invalid, but                                                          
it was valid instead

嗯,它不应该用一个nil值测试:epoch,因为另一个验证禁止使用nil。

我在这里想念什么?奇怪的是,如果我避免输入大写,它将按预期工作。

这里有两点不清楚:

  • 该消息说它对两个记录使用相同的值“虚拟值”,并且作用域不同,并且应该无效,而我认为它应该完全有效。
  • 在使用其他范围时,它使用的是无效范围(不接受nil),但消息显示所保存的记录是有效的。

0 个答案:

没有答案