First post It is a memorandum.
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
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.
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 }
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.