知識不足、わかりにくい点あるかと思いますがよろしくお願いします。
現在開発中のフリマサイトアプリにおいて商品一覧画面表示の
【products#indexアクション内で定義しているインスタンス変数があるか】
の単体テストを行なっています。先日うまくいっていたと思ったのですがまだ問題がありました。
ご教授いただければ幸いです。
上記写真のassigns(:ladies__products)がnullになってしまします。
原因としてはfactorybotが生成するcategory_idと既存のcategory_id(現状1,2で固定しています)が合わないので
assigns(:ladies__products)がnullになっているものと思われます。
products_controller.rbに記述したコントローラアクションを変更せずにproducts_controller_spec.rbの記述の変更のみでテストを証明できますでしょうか?
products_controller.rb
ruby
1class ProductsController < ApplicationController 2 3 def index 4 @ladies__products = Product.includes(:images).where(category_id: 1).order("created_at DESC").limit(10) 5 @mens__products = Product.includes(:images).where(category_id: 2).order("created_at DESC").limit(10) 6 end 7 8 def show 9 end 10 11 def new 12 end 13 14end
products_controller_spec.rb
ruby
1require 'rails_helper' 2 3describe ProductsController, type: :controller do 4 let(:products){ FactoryBot.create(:product) } 5 describe 'GET #index' do 6 it "商品一覧ページに遷移するか" do 7 get :index 8 expect(response).to render_template :index 9 end 10 it "インスタンス変数が期待したものになるか" do 11 12 13 get :index 14 binding.pry 15 expect(assigns(:ladies__products)).to match_array products 16 end 17 end 18end
#spec/factories.product.rb
ruby
1FactoryBot.define do 2 factory :product do 3 seller { FactoryBot.create(:user) } 4 buyer { FactoryBot.create(:user) } 5 name {"アクセサリー"} 6 discription {"人気商品です"} 7 category { FactoryBot.create(:category) } 8 brand {"ナイキ"} 9 state {"人気商品です"} 10 delivery_fee {"300"} 11 sending_method {"未定"} 12 sending_area {"愛知"} 13 sending_day {"三日"} 14 price {"1000"} 15 end 16end 17
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/24 04:13