Day10 测试写起乃-FactoryBot(2)

今天就来继续介绍 traitparentassociationalias

alias

简单来说就是更改 factory 名称

# users.rb
FactoryBot.define do
  factory :user, aliases: [:ck] do
    association :profile
    sequence(:name) { |n| "ck-#{n}"}
    sequence(:email) { |n| "ck-#{n}@gmail.com" }
  end
end

FactoryBot.build(:ck)
D, [2021-09-10T21:49:43.146980 #53028] DEBUG -- :    (1.8ms)  SELECT sqlite_version(*)
 => #<User id: nil, created_at: nil, updated_at: nil, name: "ck-1", email: "[email protected]", phone: nil, shop_id: nil>

parent

在 factory 里再建立一笔 factory 可以指定 parent 继承 parent 的 attributes

# profiles.rb
FactoryBot.define do
  factory :profile do
    address { "ck底加拉" }
    age { 18 }

    factory :ck_profile, parent: :profile do
      age { 2 }
    end
  end
end

2.6.6 :001 > FactoryBot.build(:ck_profile)
D, [2021-09-10T21:52:58.330179 #53106] DEBUG -- :    (1.7ms)  SELECT sqlite_version(*)
 => #<Profile id: nil, address: "ck底加拉", age: 2, user_id: nil, created_at: nil, updated_at: nil>

association

可以在建立资料时关联相关的 model 一起建立

# users.rb
FactoryBot.define do
  factory :user do
    association :profile
    sequence(:name) { |n| "ck-#{n}"}
    sequence(:email) { |n| "ck-#{n}@gmail.com" }
  end
end

# profile.rb
FactoryBot.define do
  factory :profile do
    address { "ck底加拉" }
    age { 18 }
  end
end

2.6.6 :006 > user = FactoryBot.build(:user)
 => #<User id: nil, created_at: nil, updated_at: nil, name: "ck-4", email: "[email protected]", phone: nil, shop_id: nil>
2.6.6 :007 > user.profile
 => #<Profile id: nil, address: "ck底加拉", age: 18, user_id: nil, created_at: nil, updated_at: nil>

也可以这样写

# users.rb
FactoryBot.define do
  factory :user do
    association :profile, factory: :ck_profile #找另一个profile的factory
    sequence(:name) { |n| "ck-#{n}"}
    sequence(:email) { |n| "ck-#{n}@gmail.com" }
  end
end

# profiles.rb
FactoryBot.define do
  factory :profile do
    address { "ck底加拉" }
    age { 18 }

    factory :ck_profile do
      address { "我家" }
      age { 2 }
    end
  end
end

2.6.6 :001 > FactoryBot.build(:user).profile
D, [2021-09-10T21:37:18.376241 #52655] DEBUG -- :    (1.6ms)  SELECT sqlite_version(*)
 => #<Profile id: nil, address: "我家", age: 2, user_id: nil, created_at: nil, updated_at: nil>

也可以直接覆写 attribute

# users.rb
FactoryBot.define do
  factory :user do
    association :profile, address: '库拉比卡咖啡'
    sequence(:name) { |n| "ck-#{n}"}
    sequence(:email) { |n| "ck-#{n}@gmail.com" }
  end
end

2.6.6 :001 > FactoryBot.build(:user).profile
D, [2021-09-10T21:42:43.492216 #52784] DEBUG -- :    (1.7ms)  SELECT sqlite_version(*)
 => #<Profile id: nil, address: "库拉比卡咖啡", age: 18, user_id: nil, created_at: nil, updated_at: nil>

不过因为预设得资料最好不要一直关联下去,会造成建立太多不必要的资料进而减慢测试速度所以建议使用等等介绍的 trait

trait

类似於把资料都分隔开,需要用到时再引用

# users.rb
FactoryBot.define do
  factory :user, aliases: [:ck] do
    sequence(:name) { |n| "ck-#{n}"}
    sequence(:email) { |n| "ck-#{n}@gmail.com" }

    trait :ck_user do
      association :profile
      name 'ck'
      email '[email protected]'
    end
    
    trait :normal_user do
      name 'nono'
      email '[email protected]'
    end
  end
end

2.6.6 :002 > ck = FactoryBot.build(:user, :ck_user)
 => #<User id: nil, created_at: nil, updated_at: nil, name: "ck", email: "[email protected]", phone: nil, shop_id: nil>
2.6.6 :003 > ck.profile
 => #<Profile id: nil, address: "ck底加拉", age: 18, user_id: nil, created_at: nil, updated_at: nil>

也可以一次用上多种 trait

# users.rb
FactoryBot.define do
  factory :user, aliases: [:ck] do
    sequence(:name) { |n| "ck-#{n}"}
    sequence(:email) { |n| "ck-#{n}@gmail.com" }

    trait :ck_name do
      name { 'ck' }
    end
    
    trait :ck_email do
      email { '[email protected]' }
    end
  end
end

2.6.6 :001 > ck = FactoryBot.build(:user, :ck_name, :ck_email)
D, [2021-09-10T22:05:16.143022 #53864] DEBUG -- :    (2.3ms)  SELECT sqlite_version(*)
 => #<User id: nil, created_at: nil, updated_at: nil, name: "ck", email: "[email protected]", phone: nil, shop_id: nil>

当然他可以做很多组合技,我们之後范例会慢慢使用到,明天会再继续介绍 factorybot !


<<:  10.unity摄影机跟随功能(Cinemachine)

>>:  30天零负担轻松学会制作APP介面及设计【DAY 01】

【Day 29】Hook 09:自定义 Hook(Custom hook)

打造自己的 Hook 自 React 16.8 以後, 使用者就可以在 React 中 创建自定义的...

Sklearn读取自定的CSV k-means范例修改

数据集的使用,常常令人一头雾水,举例来说,iris dataset这个最常用的资料集。 用一行代码就...

Chapter3 今天来学习画一棵树(I)学学人家DOM 自己用递回做一个树状图结构

你是说...树吗? 嘿~丢!铁人赛至今已经过半,实在是油尽灯枯,想不到主题了,刚好看到这两个很赞的树...

企划实现(18)

在撰写程序时我发现了一个以前没有遇到过的事情,我原先一直以为是因为环境导致的但是後来我发现跟环境没有...

Day 3. 关於.NET後端技术

我在接触写程序後没多久(大约半年)就开始接触後端,一开始什麽都看不懂,当时公司里的前辈虽然愿意回答问...