ruby on rails - Simple Spec That Validates_Associated -
i trying test validation on campaign
model. have form allows user create campaign
should require choose program
before can save it.
i tried following along few answers here on couldn't them work. here's have far..
the relationship..
class campaign < activerecord::base belongs_to :program validates_associated :program, message: "you need choose program." end class program < activerecord::base has_many :campaigns end
..and spec.
it 'validates associated campaign' campaign = build(:campaign) expect(campaign.save).to false expect(campaign.errors).to eq "you need choose program." end
the failure..
failures: 1) campaign validates associated campaign failure/error: expect(campaign.save).to false expected false got true # ./spec/models/campaign_spec.rb:34:in `block (2 levels) in <top (required)>'
validates_associated
works when associated object present. in example spec campaign factory (i assume) not add associated program, no validation performed, , campaign saved.
what looking validates :program, presence: true
, valid?
return false if program missing.
see more info in rails guide activerecord validations.
Comments
Post a Comment