When you run the spec 「ActionView::Template::Error:undefined method `name' for nil:NilClass」 Error occurs
spec
it 'The user registration page is displayed' do
get new_admin_staff_path
expect(response.status).to eq 200
expect(response).to render_template :new
end
factrybot
FactoryBot.define do
factory :user do
transient do
person { Gimei.name }
end
association :shop
sequence(:email) { |i| "admin#{i}@example.com" }
password { 'abc12345' }
sequence(:name) { |_i| "#{person.last.kanji} #{person.first.kanji}" }
shop_id { 1 }
end
end
In factrybot, the column with ID == 1 was specified, The id was issued every time the spec was passed, and the ID 1 did not exist. So, I didn't know the information to pull out and it was an error.
Therefore, instead of fixing the acquisition of information to ID == 1, It was solved by making the description to get the very first ID. → shop_id { Shop.first.id }
factrybot
FactoryBot.define do
factory :user do
transient do
person { Gimei.name }
end
association :shop
sequence(:email) { |i| "admin#{i}@example.com" }
password { 'abc12345' }
sequence(:name) { |_i| "#{person.last.kanji} #{person.first.kanji}" }
shop_id { Shop.first.id }
end
end
Recommended Posts