Factory already registered: user (FactoryGirl::DuplicateDefinitionError)

后端 未结 27 2210
我在风中等你
我在风中等你 2021-02-06 21:18

Description of problem: - I\'ve setup factory_girl_rails however whenever I try and load a factory it\'s trying to load it multiple times.

Envir         


        
27条回答
  •  不知归路
    2021-02-06 21:32

    https://github.com/thoughtbot/factory_girl/issues/638


    Loading factory girl into a development console will do this too:

    require 'factory_girl_rails'; reload!; FactoryGirl.factories.clear; FactoryGirl.find_definitions
    

    will raise a FactoryGirl::DuplicateDefinitionError on a sequence under Factory Girl v4.4.0.

    It seems the sequences get handled differently within FG and simply wrapping all sequences in a rescue block will solve the issue.

    For example:

      begin
        sequence :a_sequence do |n|
          n
        end
        sequence :another_sequence do |n|
          n*2
        end
      rescue FactoryGirl::DuplicateDefinitionError => e
        warn "#{e.message}"
      end
    

提交回复
热议问题