Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

factories loaded twice - already defined error #35

Closed
professor opened this issue Sep 18, 2011 · 16 comments
Closed

factories loaded twice - already defined error #35

professor opened this issue Sep 18, 2011 · 16 comments

Comments

@professor
Copy link

Short version: I was adding a new gem into my Gemfile which caused my Gemfile.lock to be updated. I'm no longer able to use rake or start-up rails because my factories are being included twice. After removing the new gem and trying different versions of gems, I was able to isolate the problem to the changes between these versions.

Fine
factory_girl (2.0.2)
factory_girl_rails (1.1.0)

Problems
factory_girl (2.1.0)
factory_girl_rails (1.2.0)

It's not clear to me how to debug this further. Any advice?

I am using rspec and am including the factories in my spec_helper. If I comment out everything in my spec_helper, I still have an issue.

Here is the output from caller.join("\n")

/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in `require'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in `block in require'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in `block in load_dependency'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:596:in `new_constants_in'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in `load_dependency'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in `require'
/Users/me/Documents/rails/my_project/spec/factories/courses.rb:1:in `<top (required)>'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:235:in `load'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:235:in `block in load'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in `block in load_dependency'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:596:in `new_constants_in'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in `load_dependency'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:235:in `load'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/factory_girl-2.1.0/lib/factory_girl/find_definitions.rb:20:in `block (2 levels) in find_definitions'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/factory_girl-2.1.0/lib/factory_girl/find_definitions.rb:19:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/factory_girl-2.1.0/lib/factory_girl/find_definitions.rb:19:in `block in find_definitions'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/factory_girl-2.1.0/lib/factory_girl/find_definitions.rb:15:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/factory_girl-2.1.0/lib/factory_girl/find_definitions.rb:15:in `find_definitions'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/factory_girl_rails-1.2.0/lib/factory_girl_rails/railtie.rb:12:in `block in <class:Railtie>'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/lazy_load_hooks.rb:34:in `call'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/lazy_load_hooks.rb:34:in `execute_hook'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/lazy_load_hooks.rb:42:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/railties-3.0.9/lib/rails/application/finisher.rb:46:in `block in <module:Finisher>'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/railties-3.0.9/lib/rails/initializable.rb:25:in `instance_exec'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/railties-3.0.9/lib/rails/initializable.rb:25:in `run'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/railties-3.0.9/lib/rails/initializable.rb:50:in `block in run_initializers'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/railties-3.0.9/lib/rails/initializable.rb:49:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/railties-3.0.9/lib/rails/initializable.rb:49:in `run_initializers'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/railties-3.0.9/lib/rails/application.rb:134:in `initialize!'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/railties-3.0.9/lib/rails/application.rb:77:in `method_missing'
/Users/me/Documents/rails/my_project/config/environment.rb:5:in `<top (required)>'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in `require'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in `block in require'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in `block in load_dependency'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:596:in `new_constants_in'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in `load_dependency'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in `require'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/railties-3.0.9/lib/rails/application.rb:103:in `require_environment!'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/railties-3.0.9/lib/rails/application.rb:218:in `block in initialize_tasks'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:636:in `block in execute'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:597:in `block in invoke_with_call_chain'
/Users/me/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:607:in `block in invoke_prerequisites'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:604:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:604:in `invoke_prerequisites'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:596:in `block in invoke_with_call_chain'
/Users/me/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:607:in `block in invoke_prerequisites'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:604:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:604:in `invoke_prerequisites'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:596:in `block in invoke_with_call_chain'
/Users/me/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:607:in `block in invoke_prerequisites'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:604:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:604:in `invoke_prerequisites'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:596:in `block in invoke_with_call_chain'
/Users/me/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:607:in `block in invoke_prerequisites'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:604:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:604:in `invoke_prerequisites'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:596:in `block in invoke_with_call_chain'
/Users/me/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:2029:in `block (2 levels) in top_level'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:2029:in `block in top_level'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:2001:in `block in run'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/gems/rake-0.8.7/bin/rake:31:in `<top (required)>'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/bin/rake:19:in `load'
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3/bin/rake:19:in `<main>
@joshuaclayton
Copy link
Contributor

What's the first line of spec/factories/courses.rb?

The only thing we did between 2.0.2 and 2.1.0 that would affect this (I think) is to add loading everything from Rails.root/factories (either the factories.rb file or the directory named factories). We changed requiring factories to load instead, but that doesn't seem like it'd be the issue here.

@jferris
Copy link
Member

jferris commented Sep 19, 2011

Are you invoking FactoryGirl.find_definitions anywhere, maybe in
spec_helper.rb or features/support/env.rb?

@professor
Copy link
Author

The first line of spec/factories/courses.rb is
require File.join(Rails.root,'spec','factories','factories.rb')

Inside my factories directory I have a factories.rb file that contains the simplest example for each model that will pass a validator (ie :course, :user). For models that have interesting test cases, I also have a file that defines factories for that class. (ie courses.rb has a series of courses it in that use :parent => :course)

I do not call find_definitions anywhere in my code.

My code still uses the Factory.define, I haven't had a chance to update to FactoryGirl.define.....gotta go do that soon!

@joshuaclayton
Copy link
Contributor

@professor, I kinda figured that's what was in there! factory_girl_rails will load all the factories for you, and it looks in a handful of spots. It checks factores.rb, factories (the directory), and then spec/factories and test/factories (both the Ruby files, then the directories). For any Ruby file or file within the directory at any nesting level, factory_girl_rails will load that file.

You'll want to remove that require from the first line, since the gem will load them all for you.

Let me know how it goes after removing it! You'll want to make sure none of the factory files require other factories, as the gem will load everything for you.

@professor
Copy link
Author

@joshuaclayton -- that sounds like a great improvement in the tool. I like it.

I did remove every reference that had this line of code in it:
require File.join(Rails.root,'spec','factories','factories.rb')

Ironically, now I get the error "Not registered: course"

In my spec/factories/courses.rb file I do reference a course which is defined in my spec/factories/factories.rb file

Factory.define :fse, :parent => :course do |c|
c.name 'Foundations of Software Engineering'
c.short_name 'FSE'
end

For each file in my /spec/factories directory, I moved the simple example into the file. :course into courses.rb, :person into people.rb, etc. When I run bundle exec rake, it makes some forward progress. It now creates the tables in the database, but then fails with a

/Users/tsedano/.rvm/gems/ruby-1.9.2-p180@rails3/gems/factory_girl-2.1.2/lib/factory_girl/registry.rb:38:in `add_as': Already defined: course (FactoryGirl::DuplicateDefinitionError)

If it's easier to look at my code, see https://github.com/professor/cmusv/tree/factory_girl_error -- but be gentle. Need that Tender Love.

(I also found it in my db/seeds.rb, but commenting out the whole file didn't help.

@joshuaclayton
Copy link
Contributor

I just pushed some changes so factories should be able to be loaded in any order. This means child factories can be loaded before parent factories, etc. If you can change your gemfile to point to:

gem 'factory_girl', :git => 'git://github.com/thoughtbot/factory_girl.git'

Once you update your bundle, everything should work just fine (leaving the requires commented out). Let me know how it goes!

@professor
Copy link
Author

Sorry for not responding sooner. I'm lucky if I get to rails programming for eight hours a week. When you first emailed me, I did a quick try with your repo and it wasn't successful. I've been wanting to try again, but haven't had a chance. I'll let you know once I have a moment of sanity. (I'm also secretly hoping that someone else has my same problem and it all magically goes away.)

=)

@pat270
Copy link

pat270 commented Oct 7, 2011

I'm going over Ruby on Rails 3 Tutorial and had the same issue. It magically went away after updating the gem.

@joshuaclayton
Copy link
Contributor

I'm going to close this since the upgrade to factory_girl 2.2 will resolve this issue. Please reopen or file another ticket if you're seeing similar issues. Thanks!

@varkerkamachi
Copy link

I just came across this same issue - in my test_helper.rb I had:

require 'factory_girl_rails'
require 'factories'

With previous versions of factory girl it was necessary to require the factories file as well. I simply commented it out and it fixed the issue.
require 'factory_girl_rails'
#require 'factories' <-- no longer necessary

@joshuaclayton
Copy link
Contributor

@varkerkamachi yep, that's correct; with factory_girl_rails, we look in a few directories by default for developers and try to load the factories automatically.

@virajkulkarni14
Copy link

@joshuaclayton
I have a slightly complex version of the problem.
We work on Rails 3.2, Ruby 1.8.7, and have installed a compatible version of factory_girl_rails 1.7.0. This was because its latest version required atleast Ruby 1.9.2 to run, so we fell back to an older version of the gem. All the related factories are in the /spec/factories/ directory.
When I run rspec after adding the following lines to my spec_helper.rb file

require 'factory_girl_rails'
Dir.glob(File.dirname(__FILE__) + "/factories/*").each do |factory|
    require factory
end

I get the Factory already registered: factory_name (FactoryGirl::DuplicateDefinitionError). But if I remove the above lines, I get an ArgumentError that the factory isn't registered. Also, I've made sure none of the factory files require other factories, as you have noted before. I've also checked any other files that may be requiring the gem, like db/seeds.rb; but none found.

I also tried editing my Gemfile like so:

gem 'factory_girl_rails', '1.7.0', :require => false, :git => 'git://github.com/thoughtbot/factory_girl.git'

but got an error saying that version of the gem doesn't exist in the Git repos.
What should I do? Please help.

@joshuaclayton
Copy link
Contributor

@virajkulkarni14 When you require 'factory_girl_rails', you won't need to require each factory manually. Can you remove the Dir.glob code, run the specs, and paste the real backtrace, as well as each factory file (if it's not to time-consuming)?

If you don't feel comfortable posting the code publicly, shoot me an email at jclayton@thoughtbot.com with a link to a private gist and I can take a look.

As an aside, if you're using :git within Bundler, I think the repo should be http://github.com/thoughtbot/factory_girl_rails.git.

Thanks!

@virajkulkarni14
Copy link

@joshuaclayton Thanks a lot, Joshua. Sorry for a late reply. Like you suggested, I removed those lines and more. After some tinkering, I tried the following changes and it worked:

We had a require 'factory_girl_rails' in the spec_helper.rb file; which I removed. I also replaced all method calls from Factory.method_name to FactoryGirl.method_name.
This seems to work now.
Thanks again!

@joshuaclayton
Copy link
Contributor

@virajkulkarni14 perfect, thanks for following up!

@itsNikolay
Copy link

itsNikolay commented Jan 19, 2018

I resolved it by removing spec/factories/xxx.rb from command line:

rspec spec/factories/xxx.rb spec/model/xxx.rb # before
rspec spec/model/xxx.rb # after

😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants