bcardarella / valid_attribute

Minimalist validation BDD for ActiveModel specs
178 stars 18 forks source link

Validates just on update #30

Closed passalini closed 10 years ago

passalini commented 10 years ago

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?

bcardarella commented 10 years ago

I would implement like this:


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 commented 10 years ago

@bcardarella thanks!!! =D

bcardarella commented 10 years ago

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