The following is the error message running rspec spec:
/factory_girl-2.1.0/lib/factory_girl/registry.rb:38:in `add_as': Already defined: user (FactoryGirl::DuplicateDefinitionError)
There are both factory_girl (2.1.0) and factory_girl_rails (1.2.0) installed. This error message disappears if factory_girl 2.1.0 is removed (caused another error of undefined method though). However the same setup has no problem running on another laptop with rspec.
Here is part of the output of bundle show command.
- cucumber (1.0.2)
- cucumber-rails (1.0.2)
- database_cleaner (0.6.7)
- diff-lcs (1.1.2)
- erubis (2.7.0)
- execjs (1.2.4)
- factory_girl (2.1.0)
- factory_girl_rails (1.2.0)
- ffi (1.0.9)
- gherkin (2.4.5)
- hike (1.2.1)
- i18n (0.6.0)
- jquery-rails (1.0.13)
- json (1.5.3)
- json_pure (1.5.3)
- mail (2.3.0)
- mime-types (1.16)
- multi_json (1.0.3)
- nokogiri (1.5.0)
- polyglot (0.3.2)
- rack (1.3.2)
- rack-cache (1.0.3)
- rack-mount (0.8.3)
- rack-ssl (1.3.2)
- rack-test (0.6.1)
- rails (3.1.0)
- railties (3.1.0)
- rake (0.9.2)
- rdoc (3.9.4)
- rspec (2.6.0)
- rspec-core (2.6.4)
- rspec-expectations (2.6.0)
- rspec-mocks (2.6.0)
- rspec-rails (2.6.1)
- rubyzip (0.9.4)
- sass (3.1.7)
- sass-rails (3.1.0.rc.6)
- selenium-webdriver (2.4.0)
- simple_form (1.4.2)
Here is part of gemfile for the factory_girl and rspec.
group :test do
# Pretty printed test output
gem "rspec-rails", ">= 2.0.0"
gem "cucumber-rails", ">=0.3.2"
gem 'webrat', ">= 0.7.2"
gem 'factory_girl_rails'
gem 'turn'开发者_如何学Go, :require => false
end
Any thoughts? Thanks.
gem 'factory_girl_rails', :require => false
that fixed it for me :)
In previous versions of Factory_Girls you were required to do
require 'factories'
in your specs/tests. With the latest Factory Girls you don't have to give the explicit require in the specs. So if you are using require 'factories' in your files remove it and try, the DuplicateDefinitionError will go away.
I had a very same issue. It means you are unintentionally loading your factories twice. You can output some text to the log from the top of your factories file to see if it is being loaded twice.
My problem was with Spork: it uses some magic to disable factory_girl_rails factory reloading and you have to load factories in a more controllable fashion in your spec_helper, but for some reason it won't work on the latest version of the gem. I've just removed Spork and it works fine now.
rake db:test:prepare
That fixed same error for me(not sure why), hope that will help somebody.
I was thinking that was spork+factory_girl_rails problem but turns out it's not. I'm using spork+fgr without troubles now.
I solved the problem by eventually getting rid of the factory_girl_rails gem. Still don't know how this happened. NO sport installed on my system.
Thanks for sharing.
Had the same problem. Turns out I had a spec that was named the same thing as my factory, and that was causing the error.
I had both spec/models/user_spec.rb and spec/factories/user_spec.rb
I renamed spec/factories/user_spec.rb to spec/factories/user_factory.rb and it ran fine.
Like Aren says, the error could be a require 'factories' Similarly, a requires in one factory for another factory could trigger the error. For example if there is a Single Table Inheritance base named Actors and a sub-classed Commenter, the Actor factory file could require a nested file for Commenter: # in file tests/factories/actors.rb require 'factories/actors/commenter.rb'
That require causes a duplicate definition because (apparently) the file has also been automatically loaded. The current docs don't make this clear though.
In my case I was double-defining a :user factory both in spec/factories.rb and in a new file that factory_girl_rails (1.4.0) created when I cranked out the model:
spec/factories/users.rb
Deleted that file and no more problem.
Also if you name your factory file like: factories/factory_user_spec.rb
rspec will think it's a spec and run it, then factory girl will require it, will will double define it.
Check that you don't have multiple factories with same name this is one of reasons which causes error
Attempting to define multiple factories with the same name will raise an error.
精彩评论