Validate associations
RSpec.describe User, type: :model do
# Association test
# ensure User model has a 1:m relationship with the Event model
it { should have_many(:events).dependent(:destroy) }
it { should belong_to(:user) }
# Validation tests
# ensure columns are present before saving
it { should validate_presence_of(:first_name) }
it { should validate_presence_of(:last_name) }
it { should validate_presence_of(:email) }
end
You can't effectively validate a bool attribute. Theoretically you validate as such:
it { should validate_inclusion_of(:notify).in_array([true, false]) }
However if you do so, RSpec will complaint:
Warning from shoulda-matchers:
You are using `validate_inclusion_of` to assert that a boolean column
allows boolean values and disallows non-boolean ones. Be aware that it
is not possible to fully test this, as boolean columns will
automatically convert non-boolean values to boolean ones. Hence, you
should consider removing this test.
************************************************************************