Validates just on update

#30 · closed · 3 comments

View on GitHub ↗

passalini

If I have `validates_presence_of :address, on: :update` how can I test it? I thought `it { should_not have_valid(:address).on(:update).when('', nil) }` , but this did not work and looking (very fast) the repo, looks like you do not have this feature or am I wrong?

Comments

bcardarella

I would implement like this: ``` ruby context 'new object' do it { should have_valid(:address).when(nil, '') } end context 'existing object' do subject { User.create } it { should_not have_valid(:address).when(nil, '') } end ``` ValidAttribute would have no way of knowing what state your model should be in when persisted. So it always just works off of the subject. It is up to you to define the state of the subject to run validation tests off of.

passalini

@bcardarella thanks!!! =D

bcardarella

Just make sure you create a valid object in the 2nd context so that the validations are fired on the persisted state (update state)