Reputation: 19290
I’m using Rails 6.2, Ruby 2.7.5 and RSpec 3.10.1. When testing an association, I’m getting a strange error…
Expected Voyage to have a belongs_to association called user (and for the record to fail validation if :user is unset; i.e., either the association should have been defined with `required: true`, or there should be a presence validation on :user)
I’m confused about what this error means. Below my model is
class Voyage < ActiveRecord::Base
…
belongs_to :user, required: true
And the spec is
describe Voyage, :type => :model do
describe 'associations' do
it { should belong_to :user }
Upvotes: 2
Views: 927
Reputation: 409
The Github link that codescaptain shared has the solution that worked for me. But, I also needed to set the associations as required
or optional
depending on situation:
it { is_expected.to belong_to(:model).required }
it { is_expected.to belong_to(:model).optional }
Upvotes: 0