简体   繁体   English

rspec 带厂女

[英]rspec with factory girl

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) ) (第 15 行指的是@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.我对工厂女孩很陌生,我似乎找不到很好地解释关联和序列的文档。

The problem here is obviously linked to the creation of your producer association.这里的问题显然与您的producer协会的创建有关。

Since you're using the old dsl, I'd suggest two solutions:由于您使用的是旧的 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

The use of after_build or after_create is really a matter of choice, depending on your tests purposes. after_buildafter_create的使用实际上是一个选择问题,具体取决于您的测试目的。

Here is a link to the new dsl lookup .这是新 dsl 查找的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM