Tuesday, 15 April 2014

ruby on rails - validate_presence_of does not produce 'can't be blank' error -



ruby on rails - validate_presence_of does not produce 'can't be blank' error -

i received error rspec when using validate_presence_of:

expected errors include "can't blank" when country_code set nil

this possible happened because of line wrote: before_validation :autofill_country_code following:

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

the test passes if remove validation. how prepare this?

given functionality you've described, code correct, test wrong. country code would never nil, "validate_presence_of" fail. should write test create sure country_code assigned before validation instead.

ruby-on-rails ruby ruby-on-rails-3 rspec

No comments:

Post a Comment