Comments (16)
Are you invoking FactoryGirl.find_definitions
anywhere, maybe in
spec_helper.rb
or features/support/env.rb
?
from factory_bot_rails.
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.
from factory_bot_rails.
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!
from factory_bot_rails.
@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.
from factory_bot_rails.
@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.
from factory_bot_rails.
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!
from factory_bot_rails.
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.)
=)
from factory_bot_rails.
I'm going over Ruby on Rails 3 Tutorial and had the same issue. It magically went away after updating the gem.
from factory_bot_rails.
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!
from factory_bot_rails.
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
from factory_bot_rails.
@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.
from factory_bot_rails.
@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.
from factory_bot_rails.
@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 [email protected] 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!
from factory_bot_rails.
@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!
from factory_bot_rails.
@virajkulkarni14 perfect, thanks for following up!
from factory_bot_rails.
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
😁
from factory_bot_rails.
Related Issues (20)
- Invalid DateTime.now field on factory creation
- Multiple Tests Fail on Vanilla `bundle exec rake` HOT 2
- Model generation ignores --force-plural HOT 3
- Rails ignoring factory HOT 2
- Install fewer gems on Travis HOT 1
- Incompatibility with Rails 6.0.3? HOT 2
- Perform DB migration will make factory_bot looking for methods in the wrong place HOT 2
- Successive associations issue when migrating to rails 6.1 HOT 3
- Switch from Travis CI to GitHub Actions
- Duplicate factories problem HOT 5
- Automatically populate attributes and types when generating factories for existing models HOT 2
- Cannot create Factory with multiple traits got (ArgumentError Exception: wrong number of arguments (given 1, expected 0)) Ruby 3.0, Rails 6.1.72, factory_bot_rails 6.1.0 HOT 2
- Dead link in CONTRIBUTING.md HOT 2
- Release new version to allow upgrade to factory_bot 6.3.0 HOT 4
- The railtie included in #419 breaks compatibility with some gems "undefined method `config' for nil:NilClass" Factory Bot > 6.2.0 HOT 5
- 6.3.0: `NoMethodError: undefined method '<' for nil:NilClass` when using a factory with traits HOT 20
- undefined method `config' for nil:NilClass after upgrading to 6.4.0 HOT 16
- Ability to disable reject_primary_key_attributes per factory HOT 11
- RubyGems is not linking the CHANGELOG file
- gemspec is missing required_ruby_version
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from factory_bot_rails.