[英]How to create a fixture ActsAsTaggableOn with FactoryGirl?
How can i create a fixture for ActsAsTaggableOn::tag Using FactoryGirl ? 如何使用FactoryGirl为ActsAsTaggableOn :: tag创建一个夹具?
I tried : 我试过了 :
/spec/factories/tags.rb /spec/factories/tags.rb
Factory.define ActsAsTaggableOn::Tag do |f|
f.sequence(:name) { |n| "titre#{n}" }
end
/spec/controllers/books_controller.rb /spec/controllers/books_controller.rb
it "should return 2 categories whith books" do
fake_tag = Factory(:tag)
...
end
I get : 我明白了:
Failure/Error: fake_tag = Factory(:tag)
ArgumentError:
Factory not registered: tag
Thanks for your help, Vincent 谢谢你的帮助,文森特
I guess you're using a quite old version of factory girl. 我想你正在使用一个很老的工厂女孩版本。 I encourage you to switch to the latest version if you're able to.
如果你能,我建议你切换到最新版本。
Answering your question, I think you need something like: 回答你的问题,我想你需要这样的东西:
Factory.define :tag, :class => ActsAsTaggableOn::Tag do |f|
f.sequence(:name) { |n| "titre#{n}" }
end
Check the Factory 1.3 doc here . 在此处查看Factory 1.3文档。 But as I told you before.
但正如我之前告诉你的那样。 Try to switch to a newer version.
尝试切换到更新的版本。
This is how I add tags (using acts-as-taggable-on
) to my user model (using factory_girl ): 这就是我如何向我的用户模型添加标签(使用
acts-as-taggable-on
)(使用factory_girl ):
FactoryGirl.define do
factory :post do
...
trait :poetry do
after(:create) { |post| post.update_attributes(tag_list: 'poetry') }
end
end
end
This way when I want to create just a regular Post
object, I write: 这样当我想创建一个普通的
Post
对象时,我写道:
post = create(:post)
but when I want to create a Post
tagged with poetry
, I write: 但是当我想创建一个用
poetry
标记的Post
时,我写道:
post = create(:post, :poetry)
And it works pretty well. 它运作得很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.