0
1

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 1 year has passed since last update.

RSpec FactoryBotの使い方とFakerについて

Last updated at Posted at 2021-07-28

Factoryとは

何かしらのデータを作る工場のこと

Fakerとは

ダミーデータを作るFakergemのこと

Faker gemを公式ドキュメントから調べられます。

FactoryBotのメソッド

FactoryBot.create(factory_name) レコード作成
FactoryBot.build(factory_name) インスタンスを作成(new)するメソッド
FactoryBot.attributes_for(factory_name) パラメーターを作成するメソッド

ex) user modelのFactoryを使う場合
FactoryBot.create(:user)  レコード作成
FactoryBot.build (:user)インスタンスを作成(new)するメソッド
FactoryBot.attributes_for(:user) パラメーターを作成するメソッド

*テストコードを書く時FactoryBotを省略できる

rails_helper.rb
config.include FactoryBot::Syntax::Methods追記

よく使うFaker

Faker::Name.name  #nameの後ろにはもっとたくさんついたりします。
Faker::Internet.email
Faker::Internet.password  #Internetの後ろはもっと色々使えます。

#まだ色々あるので調べてみてください

Faker gemを公式ドキュメントから調べられます。

0
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?