FactoryBot.の省略

実装方法

spec/rails_helper.rb

RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
end

を記述すれば良い。

環境
  • taskモデルのバリデーションチェックテスト
  • FactoryBotを使ってtaskモデルのオブジェクトを作成済
実装前

spec/model/task_spec.rb

require 'rails_helper'

RSpec.describe Task, type: :model do
  describe 'validation' do
    it 'is valid with all attributes' do
      expect(FactoryBot.build(:task)).to be_valid
    end
  end
end
実装後
require 'rails_helper'

RSpec.describe Task, type: :model do
  describe 'validation' do
    it 'is valid with all attributes' do
      expect(build(:task)).to be_valid
    end
  end
end

rails以外の実装方法もここにまとめられている。