-
-
Notifications
You must be signed in to change notification settings - Fork 365
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
Comments
What's the first line of 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 |
Are you invoking |
The first line of spec/factories/courses.rb is 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! |
@professor, I kinda figured that's what was in there! 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. |
@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: 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| 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. |
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:
Once you update your bundle, everything should work just fine (leaving the requires commented out). Let me know how it goes! |
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.) =) |
I'm going over Ruby on Rails 3 Tutorial and had the same issue. It magically went away after updating the gem. |
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! |
I just came across this same issue - in my test_helper.rb I had: require 'factory_girl_rails' 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. |
@varkerkamachi yep, that's correct; with |
@joshuaclayton
I get the I also tried editing my Gemfile like so:
but got an error saying that version of the gem doesn't exist in the Git repos. |
@virajkulkarni14 When you 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 Thanks! |
@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 |
@virajkulkarni14 perfect, thanks for following up! |
I resolved it by removing rspec spec/factories/xxx.rb spec/model/xxx.rb # before
rspec spec/model/xxx.rb # after 😁 |
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")
The text was updated successfully, but these errors were encountered: