In spec/factories/post_factory.rb:
FactoryGirl.define do
factory :post do
user
title Faker::Lorem.sentence
body Faker::Lorem.paragraphs
end
end
With that, I get this error:
... factory_girl-2.6.1/lib/factory_girl/registry.rb:39:in `add_as': Factory already registered: post (FactoryGirl::DuplicateDefinitionError)
If I change factory :post
to factory :a_post, class: Post
or any other name, it works fine. The only other factories I have defined are user and session (and those work fine).
Am I doing something wrong, or is there some internal post factory that is automatically defined which prevents me from using that name?
There are a couple of potential issues, assuming you're _100% positive_ no other post factory exists (that's the first thing I'd check). Are you using the factory_girl_rails
gem and Rails 3? If you are, make sure you're not requiring factory_girl_rails
anywhere in your spec_helper or any other file that'd be loaded during your tests. Also, make sure you're not calling FactoryGirl.find_definitions
, which will load all factories; running this twice is a surefire way to see this error. Finally, are you calling require "spec/factories/post_factory"
anywhere? factory_girl_rails
will load up all the factories so this is unnecessary.
FactoryGirl doesn't create any factories for you by default so it's not going to interfere with your factory.
Welp. Apparently when I generated the post model, a test/factories/posts.rb was created, but I was only looking in the spec directory since I use RSpec.
I removed that and everything works. Thanks.
Ah, awesome! I figured that was likely the culprit.
Most helpful comment
There are a couple of potential issues, assuming you're _100% positive_ no other post factory exists (that's the first thing I'd check). Are you using the
factory_girl_rails
gem and Rails 3? If you are, make sure you're not requiringfactory_girl_rails
anywhere in your spec_helper or any other file that'd be loaded during your tests. Also, make sure you're not callingFactoryGirl.find_definitions
, which will load all factories; running this twice is a surefire way to see this error. Finally, are you callingrequire "spec/factories/post_factory"
anywhere?factory_girl_rails
will load up all the factories so this is unnecessary.FactoryGirl doesn't create any factories for you by default so it's not going to interfere with your factory.