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

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

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

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

Q&A

解決済

1回答

1303閲覧

Railsチュートリアル13章Expected at least 1 element matching "div.pagination", found 0..

menmanegi_ayaka

総合スコア10

Ruby on Rails

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

0グッド

0クリップ

投稿2019/10/03 09:06

テストを実施していますが
エラーメッセージが出てつまずいています。

ruby

1#microposts_interface_test.rb 2 3require 'test_helper' 4 5class MicropostsInterfaceTest < ActionDispatch::IntegrationTest 6 7 def setup 8 @user = users(:michael) 9 end 10 11 test "micropost interface" do 12 log_in_as(@user) 13 get root_path 14 assert_select 'div.pagination' #<=ここでfail発生 15 # 無効な送信 16 assert_no_difference 'Micropost.count' do 17 post microposts_path, params: { micropost: { content: "" } } 18 end 19 assert_select 'div#error_explanation' 20 # 有効な送信 21 content = "This micropost really ties the room together" 22 assert_difference 'Micropost.count', 1 do 23 post microposts_path, params: { micropost: { content: content } } 24 end 25 assert_redirected_to root_url 26 follow_redirect! 27 assert_match content, response.body 28 # 投稿を削除する 29 assert_select 'a', text: 'delete' 30 first_micropost = @user.microposts.paginate(page: 1).first 31 assert_difference 'Micropost.count', -1 do 32 delete micropost_path(first_micropost) 33 end 34 # 違うユーザーのプロフィールにアクセス (削除リンクがないことを確認) 35 get user_path(users(:archer)) 36 assert_select 'a', text: 'delete', count: 0 37 end 38 test "micropost sidebar count" do 39 log_in_as(@user) 40 get root_path 41 assert_match "#{@user.microposts.count} microposts", response.body 42 # まだマイクロポストを投稿していないユーザー 43 other_user = users(:malory) 44 log_in_as(other_user) 45 get root_path 46 assert_match "0 microposts", response.body 47 other_user.microposts.create!(content: "A micropost") 48 get root_path 49 assert_match "1 micropost", response.body 50 end 51end 52
#microposts.yml orange: content: "I just ate an orange!" created_at: <%= 10.minutes.ago %> user: michael tau_manifesto: content: "Check out the @tauday site by @mhartl: http://tauday.com" created_at: <%= 3.years.ago %> user: michael cat_video: content: "Sad cats are sad: http://youtu.be/PKffm2uI4dk" created_at: <%= 2.hours.ago %> user: michael most_recent: content: "Writing a short test" created_at: <%= Time.zone.now %> user: michael #13.27 <% 30.times do |n| %> micropost_<%= n %>: content: <%= Faker::Lorem.sentence(5) %> created_at: <%= 42.days.ago %> user: michael <% end %> ants: content: "Oh, is that what you want? Because that's how you get ants!" created_at: <%= 2.years.ago %> user: archer zone: content: "Danger zone!" created_at: <%= 3.days.ago %> user: archer tone: content: "I'm sorry. Your words made sense, but your sarcastic tone did not." created_at: <%= 10.minutes.ago %> user: lana van: content: "Dude, this van's, like, rolling probable cause." created_at: <%= 4.hours.ago %> user: lana

ruby

1#microposts_controller.rb 2class MicropostsController < ApplicationController 3 before_action :logged_in_user, only: [:create, :destroy] 4 before_action :correct_user, only: :destroy #13.52 5 6 def create 7 @micropost = current_user.microposts.build(micropost_params) 8 if @micropost.save 9 flash[:success] = "Micropost created!" 10 redirect_to root_url 11 else 12 @feed_items = current_user.feed.paginate(page: params[:page]) 13 render 'static_pages/home' 14 end 15 end 16 17 def destroy 18 @micropost.destroy 19 flash[:success] = "Micropost deleted" 20 redirect_to request.referrer || root_url 21 end 22 23 private 24 def micropost_params 25 params.require(:micropost).permit(:content) 26 end 27 28 def correct_user 29 @micropost = current_user.microposts.find_by(id: params[:id]) 30 redirect_to root_url if @micropost.nil? 31 end 32end 33

html

1#show.html.erb 2<% provide(:title, @user.name) %> 3<div class="row"> 4 <aside class="col-md-4"> 5 <section class="user_info"> 6 <h1> 7 <%= gravatar_for @user %> 8 <%= @user.name %> 9 </h1> 10 </section> 11 </aside> 12 13 <div class="col-md-8"> 14 <%# <div class="div.col-md-8"> %> 15 <% if @user.microposts.any? %> 16 <h3>Microposts (<%= @user.microposts.count %>)</h3> 17 <ol class="microposts"> 18 <%# </ol.class="microposts"> %> 19 <%= render @microposts %> 20 </ol> 21 <%# </ol.class> %> 22 <%= will_paginate @microposts %> 23 <% end %> 24 </div> 25</div>

エラーメッセージ

FAIL["test_micropost_interface", MicropostsInterfaceTest, 0.8189151930000662] test_micropost_interface#MicropostsInterfaceTest (0.82s) Expected at least 1 element matching "div.pagination", found 0.. Expected 0 to be >= 1. test/integration/microposts_interface_test.rb:13:in `block in <class:MicropostsInterfaceTest>'

実際にサイトに表示すると、ページネーションは機能していました。
ご教授頂けると幸いです。
よろしくお願いいたします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

こんばんは。

実際にサイトに表示すると、ページネーションは機能していました。

とありますが、テストではなく、developmentモードで起動してブラウザで見た時にはページング用のリンクなどが表示されているということですね。

可能なら、ページングの部品が表示されている状態で、HTMLのソースがどうなっているか確認してみてください。
ブラウザの開発者ツールや、「ページのソースを表示」といったメニューで確認できると思います。

テストで指定している div.pagination は、HTMLの中に<div class="pagination">....という要素が存在することを想定しています。

実際に表示した際のページングのリンク付近に、<div class="pagination">.... というタグがあれば、なにかほかにも問題があるのかもしれません。
(fixturesで記載したテストデータが登録されていないとか)

逆に、実際に表示した際には、ページングのリンクなどは存在しても<div class="pagination">.... というタグが見当たらないのだとすると、テストをちょっと修正するかなにかが必要かと思います。

差し支えなければ、結果を教えていただけると嬉しいです。

こちらの質問ももしかしたら近いのかな?とは思っています。
https://teratail.com/questions/213274

投稿2019/10/03 11:28

suama

総合スコア1997

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

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

menmanegi_ayaka

2019/10/03 15:13

早速のコメントありがとうございます 問題が見つかりました! home.html.erb ``` <% if logged_in? %> <%= render 'static_pages/home_logged_in' %> <% else %> <%= render 'static_pages/home_not_logged_in' %> <% end %> ``` _home_logged_in.html.erb ``` <div class="row"> <aside class="col-md-4"> <section class="user_info"> <%= render 'shared/user_info' %> </section> <section> <%= render 'shared/micropost_form' %> </section> </aside> <div class="col-md-8"> <h3>Micropost Feed</h3> <%= render 'shared/feed' %> </div> </div> ``` home_logged_in.html.erbの <div class="col-md-8"> <h3>Micropost Feed</h3> <%= render 'shared/feed' %> </div> が抜けておりました show.html.erbばかり見てました お陰さまで前に進めそうです( ^∀^)
suama

2019/10/03 22:08

よかったですね!
menmanegi_ayaka

2019/10/03 22:57

@suama さん ありがとうございました!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問