5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

FactoryGirlで連番を使う

Posted at

FactoryGirlであるカラムに同一ではない適当な値を入れる際にしたこと。
※連番を使うので完全にランダムではない。

今回はCustomerが持つmail_addressを例に進めます。
「sequence」を使用するだけなのでとっても簡単。

連番

factory :customer do
   sequence(:mail_address){|n| "hoge_#{n}@fuga.com"}
end
結果
hoge_1@fuga.com,hoge_2@fuga.com,hoge_3@fuga.com・・・

初期値付き連番

factory :customer do
   sequence(:mail_address,20){|n| "hoge_#{n}@fuga.com"}
end
結果
hoge_20@fuga.com,hoge_21@fuga.com,hoge_22@fuga.com・・・

数値以外

factory :customer do
   sequence(:mail_address, 'a'){|n| "hoge_#{n}@fuga.com"}
end
結果
hoge_a@fuga.com,hoge_b@fuga.com,hoge_c@fuga.com・・・

アドレスに日本語はありえませんが日本語もできます

factory :customer do
   sequence(:mail_address, 'あ'){|n| "hoge_#{n}@fuga.com"}
end
結果
hoge_@fuga.com,hoge_@fuga.com,hoge_@fuga.com・・・
5
4
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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?