Reload factory_girl factory

后端 未结 2 1562
走了就别回头了
走了就别回头了 2021-02-19 10:50

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 understandin

相关标签:
2条回答
  • 2021-02-19 11:23

    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.

    0 讨论(0)
  • 2021-02-19 11:37

    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.

    0 讨论(0)
提交回复
热议问题