How to create an ActsAsTaggableOn fixture with FactoryGirl?

How can I create a fixture for ActsAsTaggableOn :: tag Using FactoryGirl?

I tried:

/spec/factories/tags.rb

Factory.define ActsAsTaggableOn::Tag do |f| f.sequence(:name) { |n| "titre#{n}" } end 

/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

+7
source share
2 answers

I assume that you are using a rather old version of a factory girl. I recommend that you upgrade to the latest version if you can.

When answering the question, I think you need something like:

 Factory.define :tag, :class => ActsAsTaggableOn::Tag do |f| f.sequence(:name) { |n| "titre#{n}" } end 

Check out the factory 1.3 doc here . But as I told you. Try upgrading to a newer version.

+9
source

This is how I add tags (using acts-as-taggable-on ) to my user model (using factory_girl ):

 FactoryGirl.define do factory :post do ... trait :poetry do after(:create) { |post| post.update_attributes(tag_list: 'poetry') } end end end 

Thus, when I want to create only a regular Post object, I write:

 post = create(:post) 

but when I want to create a Post with a poetry tag, I write:

 post = create(:post, :poetry) 

And it works very well.

+7
source

All Articles