Reputation: 1864
I'm using this factory file for user
model:
FactoryBot.define do
factory :user do |f|
f.sequence(:first_name) { |n| "#{Faker::Name.first_name}foo#{n}" }
f.sequence(:last_name) { |n| "#{Faker::Name.last_name}foo#{n}" }
f.sequence(:email) { |n| "foo#{n}@example.com" }
f.password "foobar"
f.password_confirmation { |u| u.password }
f.sequence(:confirmed_at) { Date.today }
f.sequence(:telephone_number) { Faker::Number.number(10) }
f.sequence(:mobile_phone_number) { Faker::Number.number(10) }
f.sequence(:verification_code) { '0000' }
f.sequence(:is_verified) { false }
end
end
and Order.rb
factory is:
FactoryBot.define do
factory :order do
association :store
association :user
total_price Faker::Number.positive
total_discount Faker::Number.positive
end
end
And the order
model should have these three FKs, two of which are from User
:
class Order < ApplicationRecord
belongs_to :customer, class_name: 'User'
belongs_to :carrier, class_name: 'User'
belongs_to :store
end
and in order_controllers_spec.rb
file, I got these:
let(:customer) { FactoryBot.create(:user) }
let(:carrier) { FactoryBot.create(:user) }
let(:store) { FactoryBot.create(:store) }
let(:order) { FactoryBot.create(:order, customer_id: customer.id, carrier_id: carrier.id, store_id: store.id) }
Each time I run the show
test,
describe "GET show" do
it 'has a 200 status code' do
get :show, params: { id: order_item.id }
expect(response.status).to eq(200)
end
end
I got this error
Failure/Error: let(:order) { FactoryBot.create(:order, customer_id: customer.id, carrier_id: carrier.id, store_id: store.id) }
NoMethodError:
undefined method `user=' for #<Order:0x00007fcd2efc5118>
Any ideas about how to solve this?
Upvotes: 0
Views: 1331
Reputation: 30071
I think in your Order
's factory definition you're using user
, instead of customer
or carrier
as your Order
model define.
association :customer, factory: :user
association :carrier, factory: :user
Upvotes: 4