工厂女工协会

发布于 2024-11-29 01:55:33 字数 769 浏览 0 评论 0原文

我编写了一个 rspec 测试,它在我的标题中添加了一些单元。

我有两个型号=>标题和单位。量规有很多单位。 看起来像这样:

@rubric.units.push Factory :text_unit
@rubric.save

然后我找到factory_girl并尝试重写 将此代码作为工厂。但它不起作用。

我怎样才能在工厂女孩中写出这个关联。 我试过这个:

  factory :common_rubric , :class => :common_info_rubric do |f|
    f.sequence(:name) {|n| "common_info_rubric#{n}"}
  end

  factory :text_unit, :class => text_info_unit do |f|
    f.association :common_rubric_with_unit
    f.sequence(:name) {|n| "text_unit#n}" }
  end

  factory :common_rubric_with_unit , :parent => :common_rubric do |f|
    f.units { |unit| unit.association(:text_info_unit) }
  end   

我总是有错误

SystemStackError:
       stack level too deep

I've written a rspec test which adds into my rubric some units.

I have two models => Rubric and Units. Rubrics have many units.
It looks like this:

@rubric.units.push Factory :text_unit
@rubric.save

Then I found factory_girl and tried to rewrite
this code as a factory. But it is not working.

How can i write this association in Factory Girl.
I tried this:

  factory :common_rubric , :class => :common_info_rubric do |f|
    f.sequence(:name) {|n| "common_info_rubric#{n}"}
  end

  factory :text_unit, :class => text_info_unit do |f|
    f.association :common_rubric_with_unit
    f.sequence(:name) {|n| "text_unit#n}" }
  end

  factory :common_rubric_with_unit , :parent => :common_rubric do |f|
    f.units { |unit| unit.association(:text_info_unit) }
  end   

I always have error

SystemStackError:
       stack level too deep

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

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

发布评论

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

评论(2

用心笑 2024-12-06 01:55:33

你那里有一个循环引用。当您创建 text_unit 时,它会创建关联的 common_rubric_with_unitcommon_rubric_with_unit 的定义创建了一个关联的 text_unit,我们又回到了起点。

您需要从任一侧删除一个关联,这应该有效:

factory :text_unit, :class => text_info_unit do |f|
  f.association :common_rubric_with_unit
  f.sequence(:name) {|n| "text_unit#n}" }
end

factory :common_rubric_with_unit , :parent => :common_rubric do |f|
end

You have a circular reference there. When you create a text_unit it creates an associated common_rubric_with_unit. The definition for common_rubric_with_unit creates an associated text_unit and we're back at the start.

You'll need to remove one of the associations from either side, this should work:

factory :text_unit, :class => text_info_unit do |f|
  f.association :common_rubric_with_unit
  f.sequence(:name) {|n| "text_unit#n}" }
end

factory :common_rubric_with_unit , :parent => :common_rubric do |f|
end
七色彩虹 2024-12-06 01:55:33

所有问题都是模型中表的非默认名称。
读完后
http://robots.thoughtbot.com/post/254496652/aint -无马蹄莲背女孩
我解决问题

 factory :common_rubric , :class => :common_info_rubric do |f|
    f.sequence(:name) {|n| "common_info_rubric#{n}"}
  end

  factory :text_unit, :class => :text_info_unit do |f|
    f.sequence(:name) {|n| "text_unit#{n}" }
  end

  factory :common_rubric_with_unit, :parent => :common_rubric do |f|
    f.after_create {|a| Factory(:text_unit, :rubric => a) }
  end

All problem was the undefault name of table in models.
And after read
http://robots.thoughtbot.com/post/254496652/aint-no-calla-back-girl
i solve the problem

 factory :common_rubric , :class => :common_info_rubric do |f|
    f.sequence(:name) {|n| "common_info_rubric#{n}"}
  end

  factory :text_unit, :class => :text_info_unit do |f|
    f.sequence(:name) {|n| "text_unit#{n}" }
  end

  factory :common_rubric_with_unit, :parent => :common_rubric do |f|
    f.after_create {|a| Factory(:text_unit, :rubric => a) }
  end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文