When I was writing the test code in Rails, even if I filled in all the necessary items, I got an error about the description of the externally referenced column, so I summarized how to describe the external reference key in FactoryBot.
In the User model and Article model as shown in the ER diagram below, consider the case where the user_id of the Article model is externally referenced. Also, as shown below, User's FactoryBot is already defined.
user.rb
#User's FactoryBot
FactoryBot.define do
factory :user do
nickname {Faker::Name.name}
email {Faker::Internet.email}
password {Faker::Internet.password}
password_confirmation {password}
last_name {"Post"}
first_name {"Taro"}
end
end
article_spec.rb
#Test code to run in Article
require 'rails_helper'
describe Article do
before do
@article = FactoryBot.build(:article)
end
describe 'Article new post' do
context 'When new posts work' do
it 'You can register if all the necessary items exist' do
expect(@article).to be_valid
end
end
context 'When new posts don't work' do
it 'Cannot register if title is empty' do
@article.title = ""
@article.valid?
expect(@article.errors.full_messages).to include("Title can't be blank")
end
end
end
end
As mentioned above, consider the case of unit test execution of the Article model. This time, for the sake of simplicity, when all the necessary items are described as a normal system, consider only the case where the title column is empty as an abnormal system.
article.rb
#Article FactoryBot
FactoryBot.define do
factory :article do
title {"AIUEO"}
prefecture_id {Faker::Number.within(range: 1..10)}
distance {Faker::Number.within(range: 10..500)}
content {"Aiue Okakikukeko"}
user_id {1}
end
end
At first, I defined FactoryBot of Article in the above article.rb and ran the test of article_spec.rb, but I got an error of'User must exist'in the part of it'Can be registered if all necessary items exist'. It happened. From this, you can see that the external reference cannot be made only by the description of user_id {1} in article_rb.
article_spec.rb
#Article FactoryBot
FactoryBot.define do
factory :article do
title {"AIUEO"}
prefecture_id {Faker::Number.within(range: 2..48)}
distance {Faker::Number.within(range: 10..500)}
content {"Aiue Okakikukeko"}
user {FactoryBot.create(:user)}
end
end
Changed the part that was set as user_id {1} to the instance created by User's FactoryBot. This made it externally visible in the articles table, and the test code in article_spec.rb ran successfully.
article_spec.rb
#Article FactoryBot
FactoryBot.define do
factory :article do
title {"AIUEO"}
prefecture_id {Faker::Number.within(range: 2..48)}
distance {Faker::Number.within(range: 10..500)}
content {"Aiue Okakikukeko"}
association :user
end
end
Next, I changed the part that was user_id {1} to the description of association: user. Again, the test code in article_spec.rb ran correctly.
When describing an external reference key in FactoryBot, I learned that it is not possible to correctly externally reference by simply describing numbers directly such as user_id {1}, and it is necessary to describe solutions ① and ②.
Recommended Posts