エラーが出るまでの経緯
第3版のrails-tutorialにおける11章を進めていた際に以下のようなテストを書いた。
users_profile_test.rb
require 'test_helper'
class UsersProfileTest < ActionDispatch::IntegrationTest
include ApplicationHelper
def setup
@user = users(:michael)
end
test "profile display" do
get user_path(@user)
assert_template 'users/show'
assert_select 'title', full_title(@user.name)
assert_select 'h1', text: @user.name
assert_select 'h1>img.gravatar'
assert_match @user.microposts.count.to_s, response.body
assert_select 'div.pagination'
@user.microposts.paginate(page: 1).each do |micropost|
assert_match micropost.content, responce.body
end
end
end
この'div.pagination'において失敗した。以下が失敗した内容である。
1) Failure:UsersProfileTest#test_profile_display [/home/ubuntu/workspace/sample_app/test/integration/users_profile_test.rb:17]:
Expected at least 1 element matching "div.pagination", found 0..
Expected 0 to be >= 1.
エラーの内容としては、”showテンプレートの中にページネーションクラスを持ったdiv要素があるのか”をチェックしています。ここでshowテンプレートの中に該当するものがあるのかを探してみると、、、
show.html.erb
<%= provide(:title, @user.name)%>
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<h1>
<%= gravatar_for @user %>
<%= @user.name %>
</h1>
</section>
</aside>
<div class="col-md-8">
<% if @user.microposts.any? %>
<h3>Microposts (<%= @user.microposts.count %>)</h3>
<ol class="microposts">
<%= render @microposts %>
</ol>
<%= will_paginate @microposts %>
<% end %>
</div>
</div>
なかったので、もう一度自分が書いたshowの中身に過不足がないか確認してみるがそのような点は見当たらなかった。
解決法
この時点では、テストを稼働させた時、失敗が出ないようにしたいのでdivのclassにpaginationを加える。
show.html.erb
<%= provide(:title, @user.name)%>
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<h1>
<%= gravatar_for @user %>
<%= @user.name %>
</h1>
</section>
</aside>
<div class="pagination col-md-8">
<% if @user.microposts.any? %>
<h3>Microposts (<%= @user.microposts.count %>)</h3>
<ol class="microposts">
<%= render @microposts %>
</ol>
<%= will_paginate @microposts %>
<% end %>
</div>
</div>
これを保存してテストを稼働させると、、、、
$ bundle exec rake test
RubyDep: WARNING: Your Ruby is outdated/buggy.
RubyDep: WARNING: Your Ruby is: 2.3.0 (buggy). Recommendation: upgrade to 2.3.1.
RubyDep: WARNING: (To disable warnings, see:http://github.com/e2/ruby_dep/wiki/Disabling-warnings )
Run options: --seed 64211
# Running:
............................................
Finished in 2.291725s, 19.1995 runs/s, 109.5245 assertions/s.
44 runs, 251 assertions, 0 failures, 0 errors, 0 skips
上手くいった。何故テストで失敗するのかは分かったが、どうしてrails-tutorial 4版の方はあの書き方でテストが上手くいくのかは分からなかった。