Faker is producing duplicate data when used in factory_girl

后端 未结 4 1743
天涯浪人
天涯浪人 2020-12-08 01:48

I\'m trying to populate some fake data into a factory using the Faker gem:

Factory.define :user do |user|
  user.first_name Faker::Name::first_name
  user.la         


        
相关标签:
4条回答
  • 2020-12-08 02:01

    A (less efficient) alternative to using sequences when you have a uniqueness validation on an attribute is to check whether a proposed value already exists and keep trying new ones until it's unique:

    FactoryGirl.define do
      factory :company do
        name do
          loop do
            possible_name = Faker::Company.name
            break possible_name unless Company.exists?(name: possible_name)
          end
        end
      end
    end
    
    0 讨论(0)
  • 2020-12-08 02:08

    For the sake of preserving the correct answer, here it is translocated from the blog, I take no credit for the answer.

    If you use the code below, faker will not churn out unique names

    Factory.define :user do |u|
      u.first_name Faker::Name.first_name
      u.last_name Faker::Name.last_name
    end
    

    However putting curly braces around faker makes it work!

    Factory.define :user do |u|
      u.first_name { Faker::Name.first_name }
      u.last_name { Faker::Name.last_name }
    end
    

    To explain why, the first example is producing the same names. It's only evaluating once. The second example evaluates every time the factory is used.

    This is due to the {} providing lazy evaluation. Essentially they are providing a proc/lambda with the Faker call as their return value.

    0 讨论(0)
  • 2020-12-08 02:11
    Factory.define :user do |user|
      user.first_name { Faker::Name::first_name }
      user.last_name { Faker::Name::last_name }
      user.sequence(:email) {|n| "user#{n}@blow.com" }
    end
    

    Try putting brackets around the fakers. see this link

    0 讨论(0)
  • 2020-12-08 02:13

    Note that Faker may still be providing duplicate data due to the limited amount of fake data available.

    For simple testing purposes and to get by uniqueness validations, I've used the following:

    sequence(:first_name) {|n| Faker::Name::first_name + " (#{n})"}
    sequence(:last_name) {|n| Faker::Name::last_name + " (#{n})"}
    
    0 讨论(0)
提交回复
热议问题