[Ruby On Rails] When a model unit test is performed with RSpec using FactoryBot, an error occurs because the foreign key is not entered.

First post It is a memorandum.

Error background

When running the test with RSpec, there was no input about the foreign key to be described in FactoryBot.

FactoryBot.define do
  factory :order_form do
    zipcode       {'123-4567'}
    prefecture_id {1}
    city          {'Ah Ward, Ah Call City'}
    address       {'Subcall 1-1-1'}
    phone_number  {'09012345678'}
  end
end

error contents

OrderForm
Product purchase
When the product purchase goes well
Can be purchased if all items exist(FAILED - 1)

Failures:

  1)OrderForm Product purchase When product purchase is successful You can purchase if all items exist
     Failure/Error: expect(@order_form).to be_valid
       expected #<OrderForm:0x00007fe44961c1e8 @zipcode="123-4567", @prefecture_id=1, @city="Ah Ward, Ah Call City", @address="Subcall 1-1-1", @phone_number="09012345678", @validation_context=nil, @errors=#<ActiveModel::Errors:0x00007fe448aeeaa0 @base=#<OrderForm:0x00007fe44961c1e8 ...>, @messages={:user_id=>["can't be blank"], :item_id=>["can't be blank"]}, @details={:user_id=>[{:error=>:blank}], :item_id=>[{:error=>:blank}]}>> to be valid, but got errors: User can't be blank, Item can't be blank
     # ./spec/models/order_form_spec.rb:10:in `block (4 levels) in <top (required)>'

Finished in 0.17606 seconds (files took 1.9 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/models/order_form_spec.rb:9 #OrderForm Product purchase When product purchase is successful You can purchase if all items exist

Here are some notable points.

@errors=#<ActiveModel::Errors:0x00007fe448aeeaa0 @base=#<OrderForm:0x00007fe44961c1e8 ...>, @messages={:user_id=>["can't be blank"], :item_id=>["can't be blank"]}, @details={:user_id=>[{:error=>:blank}], :item_id=>[{:error=>:blank}]}>> to be valid, but got errors: User can't be blank, Item can't be blank

He told me that the user_id and item_id that were used as foreign keys are blank.

Error correction

From the error content, I found that the description in FactoryBot should be corrected. All you have to do is add user_id and item_id to FactoryBot as instructed.

    user_id { FactoryBot.create(:user).id }
    item_id { FactoryBot.create(:item).id }

Modified terminal

OrderForm
Product purchase
When the product purchase goes well
Can be purchased if all items exist

Finished in 0.3807 seconds (files took 3.22 seconds to load)
1 example, 0 failures

It's a success.

Recommended Posts

[Ruby On Rails] When a model unit test is performed with RSpec using FactoryBot, an error occurs because the foreign key is not entered.
[Ruby On Rails] Error in test using RSpec MySQL client is not connected
[Ruby on Rails] Model test with RSpec
[Ruby on Rails] I get a warning when running RSpec because gem'chromedriver-helper' is deprecated.
[Ruby on Rails] Add a column with a foreign key constraint
Understand code coverage with Rspec, the Ruby on Rails test framework
[Ruby on Rails] View test with RSpec
"" Application.js "is not present in the asset pipeline" error in Ruby on Rails
[Ruby on Rails] Controller test with RSpec
Introducing Rspec, a Ruby on Rails test framework
[Ruby on rails] When executing the heroku command, bash: heroku: command not found is displayed. [Rails tutorial]
The story when the container does not start up with docker-compose up and an error occurs
[Rails] How to solve the error "undefined method` visit'" when using Capybara with Rspec
[Ruby on Rails Tutorial] Error in the test in Chapter 3
[Rails / RSpec] Write Model test (using Shoulda Matchers / FactoryBot)
FactoryBot, a solution when played with a foreign key validation
When an error occurs even though validation is not set
Create a large number of records with one command using the Ruby on Rails seeds.rb file
How to rename a model with foreign key constraints in Rails
[Rails5] Rspec -Unit test when nesting-
[RSpec] Unit test (using gem: factory_bot)
Let's unit test with [rails] Rspec!
What to do if you get an "A server is already running." Error when you try to start the rails server
[Rails] [Parent-child relationship] I want to register the foreign key in the child with nil when the parent is deleted.