LoginSignup
25
22

More than 3 years have passed since last update.

【Rails】 RSpecでNoMethodErrorと言われた人へ

Last updated at Posted at 2019-07-28

エラーの内容

ターミナルでbundle exec rspecを実行すると以下のような文が表示されてしまい、テストの判定が行われませんでした。

undefined method `build' for #<RSpec::ExampleGroups::User::Create:~~~~~>

undefined method `create' for #<RSpec::ExampleGroups::User::Create:~~~~~>

解決法

1. spec以下にフォルダを作成する

"spec/support/factory_bot.rb"を作成します。

2. factory_bot.rbに次の内容を記述する

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

3. rails_helper.rbで2のファイルを読み込む

rails_helper.rb
require 'support/factory_bot'

ここまで終えて、もう一度bundle exec rspecを実行したところ、無事にテストコードの判定を行うことができました。

参考

Stack Overflow「undefined method `build' for #<RSpec::ExampleGroups::UserName:>

以上を参考に解決することができました。ありがとうございます。

さいごに

前例探しに手間取ったので、記事として残します。同じエラーに遭遇した方の参考になれば幸いです。なお、理屈についてはいまの私では説明できないため、未来の私への覚書も兼ねています。

25
22
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
25
22