How to create a fixture ActsAsTaggableOn with FactoryGirl?
Asked Answered
N

2

7

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

Nilla answered 3/3, 2012 at 9:27 Comment(0)
U
10

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. But as I told you before. Try to switch to a newer version.

Ungley answered 20/5, 2012 at 14:56 Comment(0)
M
7

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

This way when I want to create just a regular Post object, I write:

post = create(:post)

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

post = create(:post, :poetry)

And it works pretty well.

Margheritamargi answered 15/8, 2013 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.