rspec 与工厂女孩
我有以下工厂:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题显然与
生产者
协会的创建有关。由于您使用的是旧的 dsl,我建议两种解决方案:
使用
after_build
或after_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:
The use of
after_build
orafter_create
is really a matter of choice, depending on your tests purposes.Here is a link to the new dsl lookup.