I'm trying to test an association.
I have these two classes:
class Survey < ActiveRecord::Base
has_many :users, through: :users_surveys
has_many :users_surveys
def update_or_create_result(user_id, result, target)
user_survey = users_surveys.find_or_create_by(user_id: params[:user_id])
if survey_passed?(result, target)
user_survey.completed!
end
end
end
class User < ActiveRecord::Base
has_many :surveys, through: :users_surveys
has_many :users_surveys
end
class UsersSurvey < ActiveRecord::Base
belongs_to :user
belongs_to :survey
end
How can I test with RSpec if the association between survey and user is created?
Just want to emphasize, that current nex syntax for RSpec is the following:
describe User do
it { is_expected.to have_many(:surveys) } # shortcut for expect(subject).to
end
describe Survey do
it { is_expected.to belong_to(:user) }
end
should
is considered to be old syntax for quite a few years now :)
The shoulda-matchers
gem provides some nifty matchers that test the association is declared and that the correct database fields and foreign keys exist:
describe User do
it { should have_many(:surveys) }
end
If you for some reason have gem-o-phobia and just want to test it straight off the bat you can use ActiveRecord::Reflection
to get information about the relations of a model:
describe User do
it "should have_many :surveys" do
expect(User.reflect_on_association(:surveys).macro).to eq :has_many
end
end
describe Survey do
it "should belong_to user" do
expect(Survey.reflect_on_association(:user).macro).to eq :belongs_to
expect(Survey.column_names).to include :user_id
end
end
You can also test the behavior of your models by creating an associated object - however if you have validation requirements than your specs sometimes end up somewhat muddled since your spec for A needs to fullfil the requirements for B.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With