FactoryGirl - 如何在运行测试之前在数据库中生成类别条目的层次结构?

发布于 2024-12-26 18:36:36 字数 682 浏览 1 评论 0原文

我正在建立一个电子商务网站,其核心是一个目录模块 - 它包含大约 20 个类别,每个类别有 5 至 5 个类别。 30个子类别,然后将产品与子类别相关联。

我正在尝试弄清楚如何创建父类别和父类别。然后至少有 2 个子类别与使用 FactoryGirl 的同一父类别关联。

这是我到目前为止所得到的:

Factory.define :parent_category do |f|
  f.name "MetalWork"
end

Factory.define :child_category do |f|
  f.name "Wedling"
  f.association :parent_category
  f.metatitle ""
  f.metadescription ""
end

我正在考虑使用一个序列来生成子类别名称和子类别名称。 SEO 数据,但是如果我随后使用以下方法生成 2 个 child_category 模型:

FactoryGirl.build_list(:child_category, 2)

它将创建两个单独的父类别,这不是我想要的。

至少有两个子类别很重要,因为我们需要确保与子类别模型一起保存的各种 SEO 数据都显示在正确的页面上。

有谁知道如何做到这一点?我知道这可能是一个非常新手的问题,感谢您的帮助!

I am building an ecommerce website, the core of which is a catalogue module - it contains about 20 categories each of which has between 5 & 30 subcategories, products are then associated with subcategories.

I am trying to figure out how to create a parent category & then at least 2 subcategories which are associated with the same parent using FactoryGirl.

Here is what I have so far:

Factory.define :parent_category do |f|
  f.name "MetalWork"
end

Factory.define :child_category do |f|
  f.name "Wedling"
  f.association :parent_category
  f.metatitle ""
  f.metadescription ""
end

I am thinking of using a sequence to generate the child category name & SEO data, however if I then generate 2 child_category models using:

FactoryGirl.build_list(:child_category, 2)

it will create two separate parent categories, which is not what I want.

Its important that there are at least two child categories because we need to ensure that various SEO data being saved with the child category models displays both on the correct pages.

Does anyone know how to accomplish this? I know this is probably a very newbie question, thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原来是傀儡 2025-01-02 18:36:36

您可以将哈希值传递给任何工厂调用以覆盖任何属性 - 这也适用于 build_list 调用:

parent = Factory(:category, :name => "Test Category")
FactoryGirl.build_list(:child_category, 2, :parent_category => parent)

请参阅 文档

You can pass a hash to any factory call to override any of the attributes - that goes for the build_list call too:

parent = Factory(:category, :name => "Test Category")
FactoryGirl.build_list(:child_category, 2, :parent_category => parent)

See the examples in the section "Building and creating multiple records" in the docs.

柠檬 2025-01-02 18:36:36

我会从类似的事情开始

# spec/support/factories.rb
FactoryGirl.define do
  factory :parent_category do
    name 'MetalWork'
  end

  factory :child_category do
    name 'Wedling'
    metatitle ''
    metadescription ''
  end
end

# some_controller_spec.rb
before :each do
  @parent = Factory :parent_directory
  (5 + rand(26)).times do
    # replace :parent_id with whatever attribute
    # you have to store parent category
    @child = Factory :child_directory, :parent_id => @parent.id
    # next you can fabricate your products with :category => @child
    # ...
  end
end

I'd start with something like

# spec/support/factories.rb
FactoryGirl.define do
  factory :parent_category do
    name 'MetalWork'
  end

  factory :child_category do
    name 'Wedling'
    metatitle ''
    metadescription ''
  end
end

# some_controller_spec.rb
before :each do
  @parent = Factory :parent_directory
  (5 + rand(26)).times do
    # replace :parent_id with whatever attribute
    # you have to store parent category
    @child = Factory :child_directory, :parent_id => @parent.id
    # next you can fabricate your products with :category => @child
    # ...
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文