Rails 测试驱动开发 Factory Girl 用例重构实践
FactoryGirl Seeds Don't like factory_girl because it is slow? Do you know that creating records in DB through factory_girl can take up to 50% of total spec run time? And even more!
This tiny gem helps fix that problem by reusing data preloaded before running test suite.
好处:
spec/support/login_helpers.rb
module LoginHelpers
def login_staff
before(:each) do
staff = create(:staff)
controller.send(:login_as, staff)
end
end
def login_user
before(:each) do
user = create(:user)
controller.send(:login_as, user)
end
end
def login_as_seller_user
before(:each) do
user = seed(:seller_user)
controller.send(:login_as, user)
end
end
def login_super_admin
before(:each) do
super_admin = seed(:super_admin)
controller.send(:login_as, super_admin)
end
end
def login_h_company_admin_staff
before(:each) do
h_company_admin_staff = seed(:h_company_admin_staff)
controller.send(:login_as, h_company_admin_staff)
end
end
def login_h_company_normal_staff
before(:each) do
b_company_normal_staff = seed(:h_company_normal_staff)
controller.send(:login_as, b_company_normal_staff)
end
end
end
FactoryGirl.define do
factory :staff, aliases: [:random_staff] do
mobile
password '1' * 6
name { generate(:random_name) }
trait :super_admin do
after(:create) { |staff| staff.add_role(seed(:staff_super_admin_role).name) }
end
trait :company_admin do
after(:create) { |staff| staff.add_role(seed(:staff_company_admin_role).name) }
end
trait :normal_staff do
after(:create) { |staff| staff.add_role(seed(:staff_normal_role).name) }
end
trait :h_company do
after(:create) do |staff|
staff.company = seed(:h_company)
staff.save
end
end
trait :b_company do
after(:create) do |staff|
staff.company = seed(:b_company)
staff.save
end
end
factory :super_admin, traits: [:super_admin, :h_company]
factory :h_company_admin_staff, traits: [:h_company, :company_admin]
factory :h_company_normal_staff, traits: [:h_company, :normal_staff]
factory :b_company_admin_staff, traits: [:b_company, :company_admin]
factory :b_company_normal_staff, traits: [:b_company, :normal_staff]
end
end
spec/rails_helper.rb
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
prepare_factory_girl_seed_data
end
def prepare_factory_girl_seed_data
FactoryGirl::SeedGenerator.create(:user_role_h)
FactoryGirl::SeedGenerator.create(:user_role_b)
FactoryGirl::SeedGenerator.create(:user_role_t)
FactoryGirl::SeedGenerator.create(:user_role_c)
FactoryGirl::SeedGenerator.create(:h_user)
FactoryGirl::SeedGenerator.create(:b_user)
FactoryGirl::SeedGenerator.create(:t_user)
FactoryGirl::SeedGenerator.create(:c_user)
FactoryGirl::SeedGenerator.create(:h_company)
FactoryGirl::SeedGenerator.create(:b_company)
FactoryGirl::SeedGenerator.create(:terminal_company)
FactoryGirl::SeedGenerator.create(:cloud_company)
FactoryGirl::SeedGenerator.create(:staff_super_admin_role)
FactoryGirl::SeedGenerator.create(:staff_company_admin_role)
FactoryGirl::SeedGenerator.create(:staff_normal_role)
FactoryGirl::SeedGenerator.create(:super_admin)
FactoryGirl::SeedGenerator.create(:h_company_admin_staff)
FactoryGirl::SeedGenerator.create(:h_company_normal_staff)
FactoryGirl::SeedGenerator.create(:b_company_admin_staff)
FactoryGirl::SeedGenerator.create(:b_company_normal_staff)
end