I have a profile model like so;
class Profile < ActiveRecord::Base
attr_accessible :first_name, :last_name
belongs_to :user
validates :user, presence: true
validates :first_name, presence: true, on: :update
validates :last_name, presence: true, on: :update
end
I am wanting to write some rspec tests to test the validation of first_name and last_name but I cannot see how to run profile.should_not be_valid only on update in a model test. Something like;
it "should be invalid without a first name on update" do
profile = FactoryGirl.build :profile
profile.first_name = nil
profile_should_not be_valid
end
doesn’t differentiate between an update or create action and I can’t see anything in the rspec documentation about this. Surely it’s a fairly common thing to test.
be_validin rspec just callsvalid?on the model.This builds a new model instance for
Profile, but does not commit it to the database. You would use thisprofilefor your create test. Setting:first_nametonilshould and callingprofile.should be_validshould pass.This builds and inserts the model instance for
Profileinto the database. You would use thisprofilefor your update test. Setting:first_nametonilshould and callingprofile.should be_validshould fail.