エラーの園

仕事、日常のあらゆるエラーと戦い、先人の知恵も拝借して何としても解決……したい!

NoMethodError: undefined method `build' for〜

エラー発生前の操作や設定など

FactoryBot使いたくて、Gemfileに定義して、bundle installした。
その後、下記の設定をしたが、上記エラーが出た。
spec/rails_helper.rb

Shoulda::Matchers.configure do |config|  
  config.integrate do |with|  
    with.test_framework :rspec  
    with.library :rails  
  end  
end  

エラー内容

NoMethodError: undefined method `build' for #<RSpec::ExampleGroups::[model名]:0x00007fc3f055b7a0>  

参考サイト

https://qiita.com/key_it6/items/fb94f229e9e45270c515

ありがとうございます!無事、解決しました🙇🏻‍♀️

原因

エラー発生前の操作や設定など以外にも設定が必要だった。
解決手順参照。

作業環境

$ sw_vers  
ProductName:    Mac OS X  
ProductVersion: 10.15.4    

解決手順

1.'spec/support/factory_bot.rb'を作成する。

mkdir spec/support  
touch spec/support/factory_bot.rb  

2.1.で作成したspec/support/factory_bot.rbに下記を記述する。

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

3.spec/rails_helper.rbspec/support/factory_bot.rbをrequireする。

require 'support/factory_bot'