現在開発中のフリマサイトアプリにおいて商品一覧画面表示の
【products#indexアクション内で定義しているインスタンス変数があるか】
の単体テストを行なっています。
ターミナルでbundle exec rspec spec/controllers/products_controller_spec.rbコマンドをおこなったところ、
KeyError:
Trait not registered: "seller"と怒られてしまいました。
生成したインスタンスが持っている情報にsellerが無いと言われているようですが、、アソシエーションを組んでいるカラムについてはseller_id → sellerのように書き直していいと思っていたのですが,(_idをつけるとその点を怒られます)
もっと根本の記述が間違っている気がするのですがまだ見つけることができません、、ご教授いただければ幸いです。
products_controller_spec.rb
ruby
1require 'rails_helper' 2 3describe ProductsController, type: :controller do 4 5 describe 'GET #index' do 6 it "商品一覧ページに遷移するか" do 7 get :index 8 expect(response).to render_template :index 9 end 10 it "インスタンス変数が期待したものになるか" do 11 products = FactoryBot.create_list(:product, 1) 12 get :index 13 expect(assigns(:products)).to eq products 14 end 15 end 16end
spec/factories/product.rb
ruby
1FactoryBot.define do 2 3 4 factory :product do 5 id {"1"} 6 seller 7 buyer 8 name {"アクセサリー"} 9 discription {"人気商品です"} 10 category 11 price {"1000"} 12 end 13end
models/product.rb
ruby
1class Product < ApplicationRecord 2 belongs_to :user 3 has_many :images 4 belongs_to :category 5 belongs_to :saler, class_name: "User" 6 belongs_to :buyer, class_name: "User" 7 8end
controllers/product_controller.rb
ruby
1class ProductsController < ApplicationController 2 3 def index 4 @ladies__products = Product.includes(:images).where(category_id: 1) 5 @mens__products = Product.includes(:images).where(category_id: 2) 6 end 7 8 def show 9 end 10 11 def new 12 end 13 14end

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。