Skip to content


Specs packaged as part of gem cause 'Factory already defined' error #170

andhapp opened this Issue · 10 comments

4 participants


IMHO, we don't need to include specs with a gem is installed. Because I have a factory defined for 'user' and the factory_girl(2.0.3) also define a factory with the same name. This results in the following error:

Already defined: user

Is there a reason to include the spec files in gemspec? Please enlighten.



The gem packages all the files checked into git, but it's not supposed to require definitions from the factory_girl specs when you use the gem. Are you sure you don't have a user factory defined somewhere else?


Nope. Because I only got the error when I upgraded to 2.0.3.


As a personal preference, I like specs within the gem so if I unpack the gem, I can run the test suite without having to worry about the git repository or having to check out the correct SHA of the gem I'm using. Listing spec files within the gem manifest doesn't have any adverse effects either.

As for the 'already defined: user' exception, I'm not sure where it'd be coming from. Unless you're explicitly requiring files from factory girl's test suite, there's no way these factories would be interfering with your suite.

You may want to look at every single file that's been required at runtime (with $") to see if files from factory girl's test suite are being required. If they're not, the factory is defined somewhere else.

Let me know how your investigation goes!


Same problem here - only with 2.0.3 .


Is it loading your definition file twice? Try putting a puts or something at the top of the factory file where you define the user factory.


Indeed, but only using 2.0.3


Ok, when I remove this line from spec_helper everything is fine.

Dir[Rails.root.join("spec/factories/**/*.rb")].each {|f| require f}

I can't remember why I did this in 2.0.2

@joshuaclayton joshuaclayton reopened this

With the factory_girl_rails dependency, FactoryGirl will call #find_definitions, which will load them all automatically.


Hint for people who uses cucumber + pickle + factory_girl_rails :
remember to comment this line in features/support/pickle.rb otherwise you will get "Already defined" error.

#Dir[Rails.root.join("spec/factories/**/*.rb")].each {|f| require f}'

Just to update that I was also loading the factories in an explicit way like @zoran. Got rid of it and it works fine now. Thanks.

@joshuaclayton: Thanks for the explanation. My argument is that as a gem user, I never have to run the specs. If I do want to run the specs with an intention to patch it, then I can just fork git repo. Thanks, again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.