Railstutorial: db: populate vs. factory girl

In the rail guide, why the author decided to use this (Listing 10.25): http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users

namespace :db do
  desc "Fill database with sample data"
  task :populate => :environment do
    Rake::Task['db:reset'].invoke
    User.create!(:name => "Example User",
                 :email => "example@railstutorial.org",
                 :password => "foobar",
                 :password_confirmation => "foobar")
    99.times do |n|
      name  = Faker::Name.name
      email = "example-#{n+1}@railstutorial.org"
      password  = "password"
      User.create!(:name => name,
                   :email => email,
                   :password => password,
                   :password_confirmation => password)
    end
  end
end

to populate the database with fake users, and also (Listing 7.16) http://ruby.railstutorial.org/chapters/modeling-and-viewing-users-two

Factory.define :user do |user|
  user.name                  "Michael Hartl"
  user.email                 "mhartl@example.com"
  user.password              "foobar"
  user.password_confirmation "foobar"
end

It seems that both ways create the users in the database correctly (does the factory girl create users in the database)? What is the reason for two different ways to create test users and how do they differ? When is one method more suitable than another?

+5
source share
1

Faker Factory .

rake Faker, , , . , .

Factory . , RSpec :

before(:each) do
 @user = Factory(:user)
end

@user. , , , .

+8

All Articles