solidusを利用したECサイトを作成しているのですが、
現在rspecテストを書いており上手くデータが作成できない為困っております。
scope
1add_search_scope :with_option_value do |option, value| 2 option_values = Spree::OptionValue.table_name 3 option_type_id = case option 4 when String then Spree::OptionType.find_by(name: option) || option.to_i 5 when Spree::OptionType then option.id 6 else option.to_i 7 end 8 9 conditions = "#{option_values}.name = ? AND #{option_values}.option_type_id = ?", value, option_type_id 10 11 group('spree_products.id').joins(variants_including_master: :option_values).where(conditions) 12end
上記のwith_option_value
を用いて商品を絞り込んで表示を行います。
controller
1def show 2 @products = filter_products 3end 4 5def filter_by_color(color) 6 products = Spree::Product.with_option_value('tshirt-color', color) 7 products_filter_by_color = [] 8 products.each do |product| 9 products_filter_by_color << product if product.taxons.pluck(:id).any? { |i| Spree::Taxon.find(params[:id]).self_and_descendants.pluck(:id).include?(i) } 10 end 11 @products_filter_by_color = products_filter_by_color 12end 13 14def filter_products 15 if params[:color] 16 filter_by_color(params[:color]) 17 @products_filter_by_color 18 end 19end 20
動作は一応確認が取れているのですがrspecのデータ作成が上手くできていない、もしくは書き方が違う為テストが通りません。
rspec
1describe 'カラーフィルター' do 2 let!(:taxonomy) { create(:taxonomy) } 3 let!(:taxon) { create(:taxon, taxonomy: taxonomy) } 4 let!(:product) { create(:product, name: 'RUBY ON RAILS TEE', taxons: [taxon], option_types: [option_type_1]) } 5 let!(:other_product) { create(:product, name: 'RUBY ON RAILS MUG', taxons: [taxon]) } 6 let!(:option_type_1) { create(:option_type, presentation: 'Color', name: 'tshirt-color', option_values: [option_value_1], id: 1) } 7 let!(:option_value_1) { create(:option_value, name: 'Blue', option_type_id: 1) } 8 9 it '選択した色に関連する商品が表示されること' do 10 visit potepan_category_path(taxon.id, color: 'Blue') 11 within('div.productBox') do 12 expect(page).to have_content 'RUBY ON RAILS TEE' 13 expect(page).not_to have_content 'RUBY ON RAILS MUG' 14 end 15 end 16end
おそらくwith_option_value
で上手くデータが取得できていないような気がします。
(データ作成が上手くできていない為)
ちなみにテストが落ちるのは商品自体が表示されていない為、@products
の中身がない為です。
アドバイスなど頂けたら幸いです。
あなたの回答
tips
プレビュー