英文:
Why i am getting this error in Factory: must be exist card? how can i solved it?
问题
只要我创建一个 'post',但我遇到了这个错误:
Failure/Error: let!(:post) { FactoryBot.create(:post, company_id: company.id, card_id: card.id) }
ActiveRecord::RecordInvalid:
Validation failed: must be exist card
post 模型如下:
class Post < ApplicationRecord
validates :first_title, :sub_title, :email, presence: true
belongs_to :card
delegate :max, to: :card
end
card 模型如下:
class Card < ApplicationRecord
include Discard::Model
end
工厂定义如下:
factory :post do
first_title { Faker::Name.name }
sub_title { Faker::Name.name }
email { Faker::Internet.email }
card_id {}
end
卡片工厂如下:
factory :card do
company_id {}
end
英文:
Only i want to create a 'post' but i am getting this error:
Failure/Error: let!(:post) { FactoryBot.create(:post, company_id: company.id, card_id: card.id) }
ActiveRecord::RecordInvalid:
Validation failed: must be exist card
the model of post is:
class Post < ApplicationRecord
validates :first_title, :sub_title, :email, presence: true
belongs_to :card
delegate :max, to: :card
end
and card:
class Card < ApplicationRecord
include Discard::Model
end
factories:
factory :post do
first_title { Faker::Name.name }
sub_title { Faker::Name.name }
email { Faker::Internet.email}
card_id {}
end
factory for card:
factory :card do
company_id {}
end
答案1
得分: 2
看起来你的工厂定义不够清晰。这应该是确保它正常工作的第一步。尝试像这样定义“associations”:
factory :post do
first_title { Faker::Name.name }
sub_title { Faker::Name.name }
email { Faker::Internet.email }
card
end
或者:
factory :post do
first_title { Faker::Name.name }
sub_title { Faker::Name.name }
email { Faker::Internet.email }
card { FactoryBot.create(:card) }
end
或者,你也可以尝试使用association
宏:
factory :post do
first_title { Faker::Name.name }
sub_title { Faker::Name.name }
email { Faker::Internet.email}
association :card, factory: :card
end
英文:
It seems your factories are not well defined. This should be the first thing to make sure it works fine. Try to define "associations" like this:
factory :post do
first_title { Faker::Name.name }
sub_title { Faker::Name.name }
email { Faker::Internet.email }
card
end
Or:
factory :post do
first_title { Faker::Name.name }
sub_title { Faker::Name.name }
email { Faker::Internet.email }
card { FactoryBot.create(:card) }
end
Or also, you can try using the association
macro:
factory :post do
first_title { Faker::Name.name }
sub_title { Faker::Name.name }
email { Faker::Internet.email}
association :card, factory: :card
end
答案2
得分: 1
从其他问题编译响应的代码如下:
factory :card do
association :company, factory: :company
end
factory :post do
first_title { Faker::Name.name }
sub_title { Faker::Name.name }
email { Faker::Internet.email}
transient do
company_id { FactoryBot.create(:company).id }
end
after(:build) do |post, evaluator|
card = FactoryBot.create(:card, company_id: evaluator.company_id)
post.card = card
end
end
let(:company) { FactoryBot.create :company }
let!(:post) { FactoryBot.create(:post, company_id: company.id }
let(:card) { post.card }
请注意,这是Ruby代码,包括FactoryBot和Faker库的用法。
英文:
Compiling the response from the other question:
factory :card do
association :company, factory: :company
end
factory :post do
first_title { Faker::Name.name }
sub_title { Faker::Name.name }
email { Faker::Internet.email}
transient do
company_id { FactoryBot.create(:company).id }
end
after(:build) do |post, evaluator|
card = FactoryBot.create(:card, company_id: evaluator.company_id)
post.card = card
end
end
let(:company) { FactoryBot.create :company }
let!(:post) { FactoryBot.create(:post, company_id: company.id }
let(:card) { post.card }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论