LoginSignup
3
1

More than 3 years have passed since last update.

[Ruby][FactoryBot] オリジナルのヘルパメソッドを定義する

Posted at

方法

FactoryBot::DefinitionProxy を拡張する。

spec/support/factory_helpers.rb
module FactoryHelpers
  def random_name
    name { %w[ゆの 宮子 沙英 ヒロ].sample }
  end
end

FactoryBot::DefinitionProxy.include(FactoryHelpers)
spec/factories/girls.rb
FactoryBot.define do
  factory :girl do
    random_name
    age 16
  end
end

参考

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1