至少1个元素匹配“div.pagination”,发现0 ..不起作用

时间:2016-01-03 18:20:41

标签: html ruby-on-rails testing yaml

您好我有这个问题,可能是faker无法正常工作?或者没有创建帖子?

 FAIL["test_profile_display", UsersProfileTest, 1.17947553191334]
 test_profile_display#UsersProfileTest (1.18s)
        Expected at least 1 element matching "div.pagination", found 0..
        Expected 0 to be >= 1.
        test/integration/users_profile_test.rb:17:in `block in <class:UsersProfileTest>'

这是我的user_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, response.body
    end
  end
end

这是我的micropost.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

<% 30.times do |n| %>
micropost_<%= n %>:
  content: <%= Faker::Lorem.sentence(5) %>
  created_at: <%= 42.days.ago %>
  user: michael
<% end %>

我遇到了这个问题。如果我写而不是'div.pagination'我写'div'它是有效的。有人有这个问题吗?提前谢谢!

这是_micropost.html.erb

<li id="micropost-<%= micropost.id %>">
    <%= link_to gravatar_for(micropost.user, size: 50), micropost.user %>
    <span class="user"><%= link_to micropost.user.name, micropost.user %></span> <span class="content"><%= micropost.content %></span>
    <span class="timestamp">
                            Posted <%= time_ago_in_words(micropost.created_at) %> ago. </span>
</li>

这是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= "div.col-md-8">
         <% if @user.microposts.any? %>
         <h3> Microposts (<%= @user.microposts.count %></h3>
         <ol.class = "microposts" >

             <%= render @microposts %>
         </ol.class>
         <% will_paginate @microposts %>
        <% end %>
    </div>

</div>

1 个答案:

答案 0 :(得分:0)

我刚发现错误!在show.html.erb我没有把“=”<% will_paginate @microposts %>

所以真正的代码是

<%= will_paginate @microposts %>

现在一切正常!