FactoryGirl 和工厂内的变量

发布于 2025-01-03 07:51:09 字数 337 浏览 2 评论 0原文

如果我想做类似

FactoryGirl.define do
  factory :leaf do
  end

  factory :tree do
    l = []
    leaves do
      l << Factory.build(:leaf)
      //some app logic here
      l
    end
    root l.first
  end
end

“我应该如何编写它才能使其工作”之类的事情该怎么办?

也许有人有一个关于使用 FactoryGirl 的非常复杂和不平凡的示例的链接?

谢谢。

What if I want to do something like

FactoryGirl.define do
  factory :leaf do
  end

  factory :tree do
    l = []
    leaves do
      l << Factory.build(:leaf)
      //some app logic here
      l
    end
    root l.first
  end
end

How should I write this to make it work?

And maybe somebody have a link with really complex and untrivial examples of using FactoryGirl?

Thank you.

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

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

发布评论

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

评论(1

凤舞天涯 2025-01-10 07:51:09

我不能 100% 确定我已遵循您的要求,但这行得通吗?

factory :tree do
    after_build { |tree|  
        # build your leaves here
        # ...

        tree.root l.first
    }
end

I'm not 100% sure I've followed what you're asking but would this work?

factory :tree do
    after_build { |tree|  
        # build your leaves here
        # ...

        tree.root l.first
    }
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文