Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

方法

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

参考

QUANON
あんた、マジなんだな?
http://quanon.github.io/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away