为什么在工厂中出现了”必须存在卡片”的错误?如何解决它?

huangapple go评论42阅读模式
英文:

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 } 

huangapple
  • 本文由 发表于 2023年2月16日 06:44:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定