ruby on rails - rspec not running before_save -
ruby on rails - rspec not running before_save -
i have model before_save :autofill_country_code
fill missing values. however, when run rspec test, doesn't seem run before_save.
i pried test open , saving did not update attributes.
how work around this?
it "should autofill country code" empty_country_code = '' @store = factory.build(:store, :country_code => empty_country_code) expect{ @store.save }.to change{ @store.country_code }.from('').to(1) end
the next autofill 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
do have validation set country_code? hooks performed in order:
before_validation after_validation before_save around_save before_create around_create after_create after_save
so if model invalid, before_save hook not executed. seek before_validation :autofill_country_code
instead.
ruby-on-rails ruby rspec
Comments
Post a Comment