11
13

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.

Factory Botのメソッド利用時に、"FactoryBot."を省略できる話

Last updated at Posted at 2018-10-17

Everyday Rails – RSpecによるRailsテスト入門でRspecを学習中に気づいた点をメモ。

FactoryBotのメソッドを利用する時は、

it "has a valid factory" do
  expect(FactoryBot.build(:user)).to be_valid
end

という形でFactoryBot.methodという形をよく見る。
(書籍にも書いてある&ググった時もこの形が多い)

ただこの際、"FactoryBot."の部分を省略できる
(以下FactoryBotのREADMEより引用)

# Returns a User instance that's not saved
user = build(:user)

省略するためには以下の設定が必要。
(以下railsの場合。rails以外のやり方はREADME参照)

spec/rails_helper.rb
RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
end

懸念点

省略できるのはいいが、
user = build(:user)という書き方だと、
FactoryBotのメソッドなのかどうか一見して判別つきにくい気もする。
使い分けも必要?

11
13
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
11
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?