In my application, I define that a Playground belongs to a Owner. In the create table, the owner foreign key is defined by:
t.belongs_to :owner, null: false, comment: "All managed objects have a owner"
As well as in the app/models/playground.rb
class Playground < ApplicationRecord
validates :code, presence: true,
uniqueness: { case_sensitive: false },
length: { maximum: 32 }
validates :name, presence: true
belongs_to :owner, class_name: "User", foreign_key: "owner_id"
end
Which corresponds to the specs I wrote in spec/models/playground_spec.rb:
require 'rails_helper'
RSpec.describe Playground, type: :model do
describe 'Validations'
subject {FactoryBot.build(:playground)}
it { should validate_presence_of(:code) }
it { should validate_uniqueness_of(:code).case_insensitive }
it { should validate_length_of(:code).is_at_most(32)}
it { should validate_presence_of(:name) }
it { should validate_presence_of(:owner) }
describe 'It can be created'
it 'has a valid factory' do
expect(build(:playground)).to be_valid
end
it 'is invalid without a code' do
expect(build(:playground, code: nil)).to_not be_valid
end
end
Nevertheless, when I run the test, I get the following error:
>rspec spec/models/playground_spec.rb
Failures:
1) Playground is expected to validate that :owner cannot be empty/falsy
Failure/Error: it { should validate_presence_of(:owner) }
Expected Playground to validate that :owner cannot be empty/falsy, but
this could not be proved.
After setting :owner to ‹nil›, the matcher expected the Playground to
be invalid, but it was valid instead.
# ./spec/models/playground_spec.rb:48:in `block (2 levels) in <top (required)>'
# ./spec/spec_helper.rb:55:in `block (3 levels) in <top (required)>'
# ./spec/spec_helper.rb:54:in `block (2 levels) in <top (required)>'
Finished in 4.27 seconds (files took 21.09 seconds to load)
13 examples, 1 failure
Failed examples:
rspec ./spec/models/playground_spec.rb:48 # Playground is expected to validate that :owner cannot be empty/falsy
What is the proper way to make sure my model actually checks for the existence of the foreign key ?
You are using presence
validator, but it looks like you should use belong_to
validator: https://matchers.shoulda.io/docs/v5.0.0/Shoulda/Matchers/ActiveRecord.html#belong_to-instance_method
it { should belong_to(:owner) }
instead of
it { should validate_presence_of(:owner) }
(I get caught often by this as well. It seems counter intuitive, but it is what it is I guess...)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments