【Rspec】spreeを使用したアプリでのエラー文 KeyError:  Factory not registered: の解決法

## したいこと

rpecを通したい。


##エラー文

```
Failures:

  1) Potepan::ProductsController product page get show 
     Failure/Error: let(:product) { create :product }
     
     KeyError:
       Factory not registered: "product"
     # ./spec/controllers/potepan/products_controller_spec.rb:5:in `block (3 levels) in <top (required)>'
     # ./spec/controllers/potepan/products_controller_spec.rb:7:in `block (3 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # KeyError:
     #   key not found: "product"
     #   ./spec/controllers/potepan/products_controller_spec.rb:5:in `block (3 levels) in <top (required)>'

Finished in 0.00756 seconds (files took 2.68 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/controllers/potepan/products_controller_spec.rb:9 # Potepan::ProductsController product page get show 
```

##エラーが出ているコード


```spec/controllers/potepan/products_controller_spec.rb

   require 'rails_helper'


  RSpec.describe Potepan::ProductsController, type: :controller do
    describe "product page" do
      let(:product) { create :product }
      before do
        get :show, params: { id: product.id }
      end
      it "get show " do
        expect(response).to be_successful
      end
    end
  end
```




##原因

products_controller_spec.rbの中に```require 'spree/testing_support/factories'``を記述してあげる。

require 'spree/testing_support/factoriesの記述は必ずしなければいけない様です。



##解決したコード

```spec/controllers/potepan/products_controller_spec.rb
   require 'rails_helper'
   require 'spree/testing_support/factories'     #←これを追加する

  RSpec.describe Potepan::ProductsController, type: :controller do
    describe "product page" do
      let(:product) { create :product }
      before do
        get :show, params: { id: product.id }
      end
      it "get show " do
        expect(response).to be_successful
      end
    end
  end
```





ちなみに、spec配下（spec内のファイル）は以下の様になってます。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/568461/d6601862-16e3-0e92-f233-fc366b0a188c.png)



```rails_helper.rb
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'

 Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }

begin
  ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
  puts e.to_s.strip
  exit 1
end
RSpec.configure do |config|

  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  config.use_transactional_fixtures = true

 config.infer_spec_type_from_file_location!
  
  config.filter_rails_from_backtrace!
  config.include FactoryBot::Syntax::Methods
end
```




```spec_helper.rb
RSpec.configure do |config|
config.expect_with :rspec do |expectations|

expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

config.mock_with :rspec do |mocks|

    mocks.verify_partial_doubles = true
  end
  config.shared_context_metadata_behavior = :apply_to_host_groups
end
```

##最後に

rails_helper.rb、spec_helper.rb、　の中身はデフォルトと多少異なるので心配な方はデフォルトのhelperの中身を参考にしてください。





今回の話とは別でrspecで参考になった記事
https://takkuso.hatenablog.com/entry/2018/06/19/225808