Reload factory_girl factory
Asked Answered
T

2

13

I want that when I make changes in factories, I see them in rails console without restarting all console.

I've found some lines and tested them with a poor understanding of what's going on, documentation isn't clear for me.:

FactoryGirl.reload

I've also tested:

> FactoryGirl.factories.clear
> FactoryGirl.find_definitions # also tested FactoryGirl.factories.find_definitions
                               # but NoMethodError is raised
=> ActiveRecord::RecordNotFound: Couldn't find Address with ID=277 Torphy Squares
Termagant answered 6/9, 2012 at 19:31 Comment(1)
I usually use a mere: FactoryGirl.reloadMild
T
8

I was using Faker. See the diff of changes that solved the problem:

# factories/building.rb, address is a string

- address    Faker::Address.street_address
+ address    { Faker::Address.street_address }

# factories/user.rb, email is a string

- email Faker::Internet.email
+ email { Faker::Internet.email }

And then FactoryGirl.reload works.

Termagant answered 11/9, 2012 at 16:45 Comment(2)
Right - that's a big gotcha with FactoryGirl. In the first case, the Faker::Address is evaluated just once, when the factory is loaded, and then never again. Thus it is a constant. In the second case, the code block is evaluated every time it is used, thus it changes for each time it is used. The second case is almost always what you intended, the first case is just a bunch of exposed sharp spinning blades lying there waiting to hurt someone.Loaning
This answer saved me a ton of time too. Serves me right for not RTFM (see dynamic attributes in their getting started guide).Snuff
S
18

You can also use reload! to reload the console environment but it doesn't reload the factories. Use FactoryGirl.reload to reload the Factory definitions. It (re)loads those definitions from the following locations (see the documentation):

test/factories.rb
spec/factories.rb
test/factories/*.rb
spec/factories/*.rb

The other commands you mention are used to clear and load the definitions. FactoryGirl.factories.clear clears all loaded Factory definitions, while FactoryGirl.find_definitions reloads all definitions from file.

Be sure to use the factory_girl_rails gem if your on Rails. When you've for example defined a factory :user, you can use it in your console with FactoryGirl.build(:user) or FactoryGirl.create(:user). This will return a User instance.

If that doesn't work, please post some more details.

Shouse answered 6/9, 2012 at 20:20 Comment(2)
reload! doesn't work. What do you mean it reloads the factories? For example, does it set the sequences back to square 1?Flaxseed
def self.reload reset_configuration register_default_strategies register_default_callbacks find_definitions endSubsistence
T
8

I was using Faker. See the diff of changes that solved the problem:

# factories/building.rb, address is a string

- address    Faker::Address.street_address
+ address    { Faker::Address.street_address }

# factories/user.rb, email is a string

- email Faker::Internet.email
+ email { Faker::Internet.email }

And then FactoryGirl.reload works.

Termagant answered 11/9, 2012 at 16:45 Comment(2)
Right - that's a big gotcha with FactoryGirl. In the first case, the Faker::Address is evaluated just once, when the factory is loaded, and then never again. Thus it is a constant. In the second case, the code block is evaluated every time it is used, thus it changes for each time it is used. The second case is almost always what you intended, the first case is just a bunch of exposed sharp spinning blades lying there waiting to hurt someone.Loaning
This answer saved me a ton of time too. Serves me right for not RTFM (see dynamic attributes in their getting started guide).Snuff

© 2022 - 2024 — McMap. All rights reserved.