1. Factory Girlのインストール
Gemfile
にfactory_girl_rails
を追加する。
# Gemfile group :test do gem 'factory_girl_rails' end
Bundlerを実行する
bundle install
FactoryGirlの接頭辞を省略する設定
# in spec/spec_helper.rb RSpec.configure do |config| config.include FactoryGirl::Syntax::Methods end
テスト実行時にFactoryに定義したデータを毎回読み込む設定
# in spec/spec_helper.rb RSpec.configure do |config| config.before(:all) do FactoryGirl.reload end end
設定内容の動作確認(例)
name属性を持つ、Tagテーブルにfactory girlを使ってデータを作成する
RSpec.describe Tag, type: :model do it 'is valid with name' do # FactoryGirl.createと書かずに書略して書ける tag = create(:tag) expect(tag).to be_valid end end