What is the proper way to check for a foreign key with Rails 5.2.4 / rspec 3.12?

user1185081

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 ?

Greg

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.

edited at
0

Comments

0 comments
Login to comment

Related

What is the proper way to check if a Boolean key exists in NSUserdefaults/UserDefaults

What's the proper way to check if JSON has a key?

what's the proper way to check if terminating AWS EC2 instance is successful using boto3?

Foreign key not saving rails 5

Rails 3 + rspec + postgres: How to run test suite against a database with foreign key constraints?

What's the proper way to check if a constant is defined?

What is the proper way to check for null values?

What is the proper way to check if an Iterator is complete?

Whats the proper way to do Rspec with files or images on rails?

What's the best way to convert a [[T; 4]; 3] into a [T; 12]?

What is the proper way to sign in as a user in an rspec request spec, without devise?

what is the proper way to get bootstrap 4 to work

What is the proper way to close H2?

What is the proper way to stop hiveserver2?

What is the proper way of combining 2 documents in MongoDB

In Swift 2, what is the proper way to disable a NSMenuItem?

What is the proper way to handle concatenation of data returned from multiple http observables in angular2/4 rxjs?

Rails 4, rspec 3: model validation test

Test layout in rails 4 with Rspec 3

Rspec Test validations of 'on: : update' not working (Rails4/Rspec 3)

What is the proper way to delay a service worker update check?

What is the proper way to check for existence of variable in an EJS template (using ExpressJS)?

What is the proper way to check if a document in mongodb with find().limit()?

Multiple check for "None" - what would be the proper way in Python

What is the proper way to check string or list of strings length in JS?

What is the proper way to check multiple form values from the template in Angular?

What is a proper way of checking if a Scala Map key doesn't exist?

What is the proper way to remove a key-value pair in typescript

What is proper way to define define-key in emacs