I am trying to test a validation on my Campaign
model. I have a form that allows the user to Create a Campaign
which should require that they choose a program
before they can save it.
I tried following along a few answers here on SO but couldn't get them to work. Here's what I have so far..
The relationship..
class Campaign < ActiveRecord::Base
belongs_to :program
validates_associated :program,
message: "You need to choose a Program."
end
class Program < ActiveRecord::Base
has_many :campaigns
end
..and the spec.
it 'validates associated campaign' do
campaign = build(:campaign)
expect(campaign.save).to be false
expect(campaign.errors).to eq "You need to choose a Program."
end
The failure..
Failures:
1) Campaign validates associated campaign
Failure/Error: expect(campaign.save).to be false
expected false
got true
# ./spec/models/campaign_spec.rb:34:in `block (2 levels) in <top (required)>'
validates_associated
only works when an associated object is present. In your example spec the campaign factory (I assume) does not add an associated program, thus no validation is performed, and the campaign is saved.
What you are looking for is validates :program, presence: true
, for which valid?
will return false if the program is missing.
See more info in the Rails Guide for ActiveRecord Validations.