【发布时间】:2014-11-10 02:52:25
【问题描述】:
我在使用 validate_presence_of 时从我的 rspec 收到此错误:
Expected errors to include "can't be blank" when country_code is set to nil
这可能是因为我写的这行代码:before_validation :autofill_country_code
它执行以下操作:
def autofill_country_code
unless self.country_code.present?
country = GeoCache.geocode(self.city).country_code
country_code = IsoCountryCodes.find(country).calling
self.country_code = country_code.tr('+', '')
end
end
如果我删除此验证,则测试通过。我该如何解决这个问题?
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 rspec