rspec 与工厂女孩

发布于 2024-11-29 11:11:33 字数 1131 浏览 2 评论 0原文

我有以下工厂:

Factory.define :producer, :class => User do |f|
  f.sequence(:email) { |n| "producer_#{n}@shit.com" }
  f.password  "foobar"
  f.password_confirmation "foobar"
  f.role      "producer"
end

Factory.define :business do |f|
  f.sequence(:name) { |n| "business_#{n}" }
  f.association(:producer, :factory => :producer)
end

Factory.define :deal do |d| 
  d.sequence(:title) { |n| "deal_#{n}" }
  d.sequence(:desc) { |n| "deal_desc_#{n}" }
  d.cap "50" 
  d.rate "2" 
  d.start Date.today - 1     # This is the date where you put in db
  d.end Date.today + 7
  d.association :business
end

现在当我执行以下操作时:

  before(:each) do 
    @consumer = test_sign_in(Factory(:consumer))
    @deal = Factory(:deal)
  end

我收到错误:(

 Failure/Error: @deal = Factory(:deal)
 NoMethodError:
   undefined method `producer=' for #<Business:0x007fb494290090>
 # ./deals_controller_spec.rb:15:in `block (4 levels) in <top (required)>

第 15 行指的是 @deal = Factory(:deal)

有谁知道为什么?我对工厂女孩很陌生,我似乎找不到很好地解释关联和顺序的文档。

I have the following factories:

Factory.define :producer, :class => User do |f|
  f.sequence(:email) { |n| "producer_#{n}@shit.com" }
  f.password  "foobar"
  f.password_confirmation "foobar"
  f.role      "producer"
end

Factory.define :business do |f|
  f.sequence(:name) { |n| "business_#{n}" }
  f.association(:producer, :factory => :producer)
end

Factory.define :deal do |d| 
  d.sequence(:title) { |n| "deal_#{n}" }
  d.sequence(:desc) { |n| "deal_desc_#{n}" }
  d.cap "50" 
  d.rate "2" 
  d.start Date.today - 1     # This is the date where you put in db
  d.end Date.today + 7
  d.association :business
end

now when I do the following:

  before(:each) do 
    @consumer = test_sign_in(Factory(:consumer))
    @deal = Factory(:deal)
  end

I am getting an error:

 Failure/Error: @deal = Factory(:deal)
 NoMethodError:
   undefined method `producer=' for #<Business:0x007fb494290090>
 # ./deals_controller_spec.rb:15:in `block (4 levels) in <top (required)>

(Line 15 refers to @deal = Factory(:deal) )

Does anyone know why? I am very new to factory girl and I can't seem to find the documentation explaining association and sequence very well.

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

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

发布评论

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

评论(1

我很OK 2024-12-06 11:11:33

这里的问题显然与生产者协会的创建有关。

由于您使用的是旧的 dsl,我建议两种解决方案:

Factory.define :business do |f|
  f.sequence(:name) { |n| "business_#{n}" }
  #try this:
  f.association(:user, :factory => :producer)
  #or this:
  f.after_build { |biz| biz.user = Factory.build(:producer) }
end

使用 after_buildafter_create 实际上是一个选择问题,具体取决于您的测试目的。

这里是新 dsl 查找的链接

The problem here is obviously linked to the creation of your producer association.

Since you're using the old dsl, I'd suggest two solutions:

Factory.define :business do |f|
  f.sequence(:name) { |n| "business_#{n}" }
  #try this:
  f.association(:user, :factory => :producer)
  #or this:
  f.after_build { |biz| biz.user = Factory.build(:producer) }
end

The use of after_build or after_create is really a matter of choice, depending on your tests purposes.

Here is a link to the new dsl lookup.

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