RailsTutorial Ch 9在分页和删除链接上测试失败

时间:2015-05-18 00:48:09

标签: ruby ruby-on-rails-4 railstutorial.org will-paginate minitest

通过rails教程并坚持第9章了解最后一个错误。

FAIL["test_index_as_admin_including_pagination_and_delete_links", UsersIndexTest, 1.799453]
 test_index_as_admin_including_pagination_and_delete_links#UsersIndexTest (1.80s)
        <delete> expected but was
        <User 19>..
        Expected 0 to be >= 1.
        test/integration/users_index_test.rb:18:in `block (2 levels) in <class:UsersIndexTest>'
        test/integration/users_index_test.rb:15:in `block in <class:UsersIndexTest>'

以下是users_index_test.rb

的测试版块
test "index as admin including pagination and delete links" do
    log_in_as(@admin)
    get users_path
    assert_template 'users/index'
    assert_select 'div.pagination'
    first_page_of_users = User.paginate(page: 1)
    first_page_of_users.each do |user|
      assert_select 'a[href=?]', user_path(user), text: user.name
      unless user == @admin
        assert_select 'a[href=?]', user_path(user), text: 'delete'
      end
    end
    assert_difference 'User.count', -1 do
      delete user_path(@non_admin)
    end
  end

1 个答案:

答案 0 :(得分:0)

我的app/views/users/_users.html.erb中有一个拼写错误,修复它帮助我通过了测试。

正确的代码是 -

<li>
  <%= gravatar_for user, size: 50 %>
  <%= link_to user.name, user %>
  <% if current_user.admin? && !current_user?(user) %>
    | <%= link_to "delete", user, method: :delete,
                                  data: { confirm: "You sure?" } %>
  <% end %>
</li>