質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

Q&A

解決済

1回答

3987閲覧

solidusのRSpecが通らない

mocchann

総合スコア4

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

0グッド

0クリップ

投稿2021/06/09 08:55

solidus+RailsでECサイト構築をしていますが、RSpecが通りません。

Dockerを使い、開発しています。
'一覧ページへ戻る'リンクをクリックするテストを実装中に以下のエラーメッセージが発生しました。

発生している問題・エラーメッセージ

Failures: 1) Categories return to list page Failure/Error: @taxon = Spree::Taxon.find(params[:id]) ActiveRecord::RecordNotFound: Couldn't find Spree::Taxon with 'id'=5723 # ./app/controllers/potepan/categories_controller.rb:3:in `show' # ./spec/features/categories_spec.rb:63:in `block (2 levels) in <top (required)>' Finished in 30.3 seconds (files took 4.14 seconds to load) 16 examples, 1 failure Failed examples: rspec ./spec/features/categories_spec.rb:59 # Categories return to list page ERROR: 1

該当のソースコード /spec/features/categories_spec.rb

rspec

1require 'rails_helper' 2 3RSpec.feature "Categories", type: :feature do 4 given(:taxonomy) { create(:taxonomy, name: 'Category') } 5 given(:taxon) { taxonomy.root } 6 given(:bag) { create(:taxon, name: 'Bag', taxonomy: taxonomy, parent: taxon) } 7 given(:clothing) { create(:taxon, name: 'Clothing', taxonomy: taxonomy, parent: taxon) } 8 given(:categories) { create(:taxon, name: 'Shirts', taxonomy: taxonomy, parent: clothing) } 9 given!(:product1) { create(:product, name: 'bag', price: 10, taxons: [bag]) } 10 given!(:product2) { create(:product, name: 'shirts', price: 20, taxons: [categories]) } 11 12 background do 13 visit potepan_category_path(categories.id) 14 end 15 16 scenario "display correct categories page" do 17 expect(page).to have_title categories.name 18 19 within '.side-nav' do 20 expect(page).to have_content taxonomy.name 21 expect(page).to have_content bag.name 22 expect(page).to have_content categories.name 23 expect(page).not_to have_content clothing.name 24 end 25 26 within '.productBox' do 27 expect(page).to have_content product2.name 28 expect(page).to have_content product2.display_price 29 expect(page).not_to have_content product1.name 30 expect(page).not_to have_content product1.display_price 31 end 32 end 33 34 scenario "move correct page from category link" do 35 click_on bag.name 36 expect(current_path).to eq potepan_category_path(bag.id) 37 38 within '.productBox' do 39 expect(page).to have_content product1.name 40 expect(page).to have_content product1.display_price 41 expect(page).not_to have_content product2.name 42 expect(page).not_to have_content product2.display_price 43 end 44 end 45 46 scenario "move correct page from product link" do 47 click_on product2.name 48 expect(current_path).to eq potepan_product_path(product2.id) 49 50 within '.singleProduct' do 51 expect(page).to have_content product2.name 52 expect(page).to have_content product2.display_price 53 expect(page).not_to have_content product1.name 54 expect(page).not_to have_content product1.display_price 55 end 56 end 57 58 scenario "return to list page" do 59 visit potepan_product_path(product1.id) 60 within '.fa-reply' 61 binding.pry 62 click_on '一覧ページへ戻る' 63 expect(current_path).to eq potepan_category_path(taxon.id) 64 end 65end 66

該当のソースコード CategoriesController

controller

1class Potepan::CategoriesController < ApplicationController 2 def show 3 @taxon = Spree::Taxon.find(params[:id]) 4 @taxonomies = Spree::Taxonomy.includes(:root) 5 @products = Spree::Product.in_taxon(@taxon).includes(master: [:images, :default_price]) 6 end 7end 8

該当のソースコード _products_details.html.erb

view

1<ul class="list-inline"> 2 <li> 3 <%= link_to potepan_category_path do %> 4 <i class="fa fa-reply" aria-hidden="true"></i>一覧ページへ戻る 5 <% end %> 6 </li> 7</ul> 8<h2><%= product.name %></h2> 9<h3><%= product.display_price %></h3> 10<p><%= product.description %></p> 11<span class="quick-drop"> 12 <select name="guiest_id3" id="guiest_id3" class="select-drop"> 13 <option value="0">S</option> 14 <option value="1">M</option> 15 <option value="2">L</option> 16 <option value="3">XL</option> 17 </select> 18</span> 19<span class="quick-drop resizeWidth"> 20 <select name="guiest_id4" id="guiest_id4" class="select-drop"> 21 <option value="1">1</option> 22 <option value="2">2</option> 23 <option value="3">3</option> 24 </select> 25</span> 26<div class="btn-area"> 27 <%= link_to potepan_cart_page_path, class: "btn btn-primary btn-block" do %> 28 カートへ入れる<i class="fa fa-angle-right" aria-hidden="true"></i> 29 <% end %> 30</div> 31<div class="tabArea"> 32 <ul class="nav nav-tabs"> 33 <li class="active"><a data-toggle="tab" href="#details">details</a></li> 34 <li><a data-toggle="tab" href="#about-art">about art</a></li> 35 <li><a data-toggle="tab" href="#sizing">sizing</a></li> 36 <li><a data-toggle="tab" href="#shipping">shipping</a></li> 37 </ul> 38 <div class="tab-content"> 39 <div id="details" class="tab-pane fade in active"> 40 <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> 41 <ul class="list-unstyled"> 42 <li>Black, Crew Neck</li> 43 <li>75% Cotton, 25% Rayon</li> 44 <li>Waterbased Ink</li> 45 <li>Wash Cold, dry low</li> 46 </ul> 47 </div> 48 <div id="about-art" class="tab-pane fade"> 49 <p>Nulla facilisi. Mauris efficitur, massa et iaculis accumsan, mauris velit ultrices purus, quis condimentum nibh dolor ut tortor. Donec egestas tortor quis mattis gravida. Ut efficitur elit vitae dignissim volutpat. </p> 50 </div> 51 <div id="sizing" class="tab-pane fade"> 52 <p>Praesent dui felis, gravida a auctor at, facilisis commodo ipsum. Cras eu faucibus justo. Nullam varius cursus nisi, sed elementum sem sagittis at. Nulla tellus massa, vestibulum a commodo facilisis, pulvinar convallis nunc.</p> 53 </div> 54 <div id="shipping" class="tab-pane fade"> 55 <p>Mauris lobortis augue ex, ut faucibus nisi mollis ac. Sed volutpat scelerisque ex ut ullamcorper. Cras at velit quis sapien dapibus laoreet a id odio. Sed sit amet accumsan ante, eu congue metus. Aenean eros tortor, cursus quis feugiat sed, vestibulum vel purus.</p> 56 </div> 57 </div> 58</div> 59

試したこと

エラーメッセージを元に調べてみたところ、id:5723のTaxonがないということなので、渡すidが間違えているか、作るべきTaxonデータがないのかという2点を考えて、binding.pryで動きを追ってみました。
controllerのインスタンス変数を確認すると、下記のような結果になりました。

pry

1From: /potepanec/spec/features/categories_spec.rb:62 : 2 3 57: 4 58: scenario "return to list page" do 5 59: visit potepan_product_path(product1.id) 6 60: within '.fa-reply' 7 61: binding.pry 8 => 62: click_on '一覧ページへ戻る' 9 63: expect(current_path).to eq potepan_category_path(taxon.id) 10 64: end 11 65: 12 66: # senario "the number of products matches the number of products listed" do 13 67: 14 15[1] pry(#<RSpec::ExampleGroups::Categories>)> Spree::Taxon.find(params[:id]) 16NameError: undefined local variable or method `params' for #<RSpec::ExampleGroups::Categories:0x000055d87ddda670> 17from /potepanec/vendor/bundle/ruby/2.5.0/gems/rspec-expectations-3.10.1/lib/rspec/matchers.rb:965:in `method_missing' 18[2] pry(#<RSpec::ExampleGroups::Categories>)> Spree::Taxonomy.includes(:root) 19=> [#<Spree::Taxonomy:0x000055d87e1ebb38 20 id: 2581, 21 name: "Category", 22 created_at: Wed, 09 Jun 2021 17:41:52 JST +09:00, 23 updated_at: Wed, 09 Jun 2021 17:41:52 JST +09:00, 24 position: 1>] 25[3] pry(#<RSpec::ExampleGroups::Categories>)> Spree::Product.in_taxon(@taxon).includes(master: [:images, :default_price]) 26NoMethodError: undefined method `self_and_descendants' for nil:NilClass 27from /potepanec/vendor/bundle/ruby/2.5.0/gems/solidus_core-2.9.6/app/models/spree/product/scopes.rb:67:in `block in <class:Product>' 28[4] pry(#<RSpec::ExampleGroups::Categories>)> potepan_product_path(product1.id) 29=> "/potepan/products/5745" 30[5] pry(#<RSpec::ExampleGroups::Categories>)> current_path 31=> "/potepan/products/5745" 32[6] pry(#<RSpec::ExampleGroups::Categories>)> potepan_category_path(taxon.id) 33=> "/potepan/categories/6690" 34[7] pry(#<RSpec::ExampleGroups::Categories>)>

私自身、solidusの仕組みを理解できていないのと、テストに関する理解も甘いので、エラーの原因を突き止めることができません。
渡すidが間違えているとしたら、どのidを渡せばテストが通るのか。(試しにrspecで定義している変数は全て渡してみましたが、全部同じエラーになりました。)
そもそもTaxonデータを作らないといけないのか。(このデータの作り方がわかりません。)
それとも他に原因があるのか。

わかる方おられましたら、ぜひご教授をよろしくお願い致します。
提示した情報以外に、必要なものがあればすぐに追加致します。

補足情報(FW/ツールのバージョンなど)

もうすぐ(6月14日)でスクール受講期間が終了してしまいますので、ぜひ回答にご協力お願い致しますm(_ _)m

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

seed値を固定しないとidが固定されませんので5723という数値に意味はありません。

問題としては

<%= link_to potepan_category_path do %>

ここでidを省略しているため、当該ページのproductのidが渡されているようです。

では、何を渡すか?は難しいです。
というのもおそらく商品とカテゴリは多対多なので、どのカテゴリの一覧を表示するかは一意に定まりません。
今回のテストを通したいだけ(多taxonを考慮しない)であれば
<%= link_to potepan_category_path(product.taxons.first) do %>
も可能でしょう。

amazon風にURLにtaxon名を渡してやるのが一番賢そうですがroutingなどでちょっと苦労します。

投稿2021/06/09 09:47

asm

総合スコア15147

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mocchann

2021/06/09 10:59

早速のご回答ありがとうございます。 もう1度お聞きしたいのですがよろしいでしょうかm(_ _)m ご回答頂いた通り、link_toにidを渡したところ以下のエラーが発生しました。 Failures: 1) Categories return to list page Failure/Error: @taxon = Spree::Taxon.find(params[:id]) ActiveRecord::RecordNotFound: Couldn't find Spree::Taxon with 'id'=category/bag # ./app/controllers/potepan/categories_controller.rb:3:in `show' # ./spec/features/categories_spec.rb:61:in `block (2 levels) in <top (required)>' この「Couldn't find Spree::Taxon with 'id'=category/bag」 の部分ですが、最後にidではなくproduct1変数のbagというnameが入っているように見えるのですが、グーグルで検索してみてもそれらしい記事がなく、また詰まってしましました。。 この部分「Couldn't find Spree::Taxon with 'id'=category/bag」は何を表しているのでしょうか??
asm

2021/06/09 12:00

なるほど、 potepan_category_path(product.taxons.first.id) のようですね (ドキュメント見るとspree.nested_taxons_path使うような事書かれてはいますが)
mocchann

2021/06/09 12:37

potepan_category_path(product.taxons.first.id) こちらに変更したところコントローラーが正常に動きました! asmさん、ありがとうございます!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問