FactoryGirl.find_definitions gives me an error
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Given I'm in Console, When I type FactoryGirl.find_definitions, Then I get this error

irb(main):001:0> FactoryGirl.find_definitions
FactoryGirl::DuplicateDefinitionError: Factory already registered: entry

Pull requests accepted: https://github.com/akshatpradhan/spoutlets

awarded to priyaaank

Crowdsource coding tasks.

1 Solution

Winning solution


I have submitted a pull request here to fix the factory definitions

There are couple of things I'd like to point out:

  • Factory girl has been revamped in recent times and old convention of Factory has been replaced by FactoryGirl now. So be careful when you google the suggestions online as lot of those still indicate Factory for usage.
  • Earlier Factory(:entry) use to implicitly mean that it would create an instance of entry object. Now that declarations has be be implicit by FactoryGirl.create(:entry)
  • FactoryGirl.build(:entry) is faster as it doesn't persist to database and useful when you are asserting behavior on non-persisted object
  • In case of rails, the lookup of factory definitions is automagical, so you don't need to do anything in your app if you have kept factory definitions in spec/factories/model_name.rb style
  • In rails console, console loads everything including gems/libraries for that environment. since rails console starts console in development mode; and factories are to be loaded in development mode; all the definitions are already loaded. all you need to do is say FactoryGirl.build(:entry) in console to build an instance of factory. Other factory operations/initializations can be performed as well.

I have ensured that this works in console right now. If it doesn't let me know. find_definition is required for non-rails app where you intend to load definitions from the folder where you have kept them and configured. Like I said, this is not required in rails.

More details here around usage and configurations:

Remember factory-girl is for rails versions prior to 3; Rails 3 onwards factory-girl-rails should be used; which you have used correctly in your project.

Did the changes here help you?
priyaaank over 6 years ago