Rails教程 - 调试bundle exec rake测试

时间:2015-12-22 20:24:21

标签: ruby-on-rails railstutorial.org

我是新用户,所以请不要杀了我。我一直在研究Michael Hartl的Rails教程,第9章,取得了一些成功。在运行“bundle exec rake test”命令时,我遇到了3个错误。这些不会影响测试的状态,所以我的测试仍然是Hartl规范的红色或绿色,但是我的网页在网页上出现了一些问题,我担心错误可能会滚雪球。

有谁知道这里似乎发生了什么?我已尽最大努力将他的代码块放在Cloud9 IDE中,因此语法应该是100%准确的正确。我的背景是Java和C#,这些让我想起了编译器错误。

1)错误:

UsersLoginTest#test_login_with_valid_information_followed_by_logout: NoMethodError: undefined method `forget' for nil:NilClass
    app/helpers/sessions_helper.rb:41:in `forget'
    app/helpers/sessions_helper.rb:48:in `log_out'
    app/controllers/sessions_controller.rb:18:in `destroy'
    test/integration/users_login_test.rb:33:in `block in <class:UsersLoginTest>'

2)错误:

UsersIndexTest#test_index_as_a_non-admin: SyntaxError: /home/ubuntu/workspace/sample_app/app/views/users/index.html.erb:12: syntax error, unexpected keyword_ensure, expecting end-of-input
    test/integration/users_index_test.rb:31:in `block in <class:UsersIndexTest>'

3)错误:

UsersIndexTest#test_index_as_admin_including_pagination_and_delete_links:
NoMethodError: undefined method `email' for nil:NilClass
    test/test_helper.rb:19:in `log_in_as'
    test/integration/users_index_test.rb:11:in `block in <class:UsersIndexTest>'

37 runs, 84 assertions, 0 failures, 3 errors, 0 skips

测试/模型/ test_helper.rb中

<% provide(:title, 'All users') %>

<h1>All users</h1>

<%= will_paginate %>

<ul class="users">
  <%= render @users %>
  <% end %>
</ul>

<%= will_paginate %>

测试/集成/ users_index_test.rb

require 'test_helper'

class UsersIndexTest < ActionDispatch::IntegrationTest

  def setup
    @admin     = users(:michael)
    @non_admin = users(:archer)
  end

  test "index as admin including pagination and delete links" do
    log_in_as(@user)
    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',
                                                    method: :delete

      end
    end
    assert_difference 'User.count', -1 do
      delete user_path(@non_admin)
    end
  end

  test "index as a non-admin" do
    log_in_as(@non_admin)
    get users_path
    assert_select 'a', text: 'delete', count: 0
  end
end

测试/集成/ users_login_test.rb

require 'test_helper'

class UsersLoginTest < ActionDispatch::IntegrationTest

  def setup
    @user = users(:michael)
  end

  test "login with invalid information" do  
    get login_path
    assert_template 'sessions/new'
    post login_path, session: { email: "", password: "" }
    assert_template 'sessions/new'
    assert_not flash.empty?
    get root_path
    assert flash.empty?
  end

  test "login with valid information followed by logout" do
    get login_path
    post login_path, session: { email: @user.email, password: 'password' }
    assert is_logged_in?
    assert_redirected_to @user
    follow_redirect!
    assert_template 'users/show'
    assert_select "a[href=?]", login_path, count: 0
    assert_select "a[href=?]", logout_path
    assert_select "a[href=?]", user_path(@user)
    delete logout_path
    assert_not is_logged_in?
    assert_redirected_to root_url
    # Simulate a user clicking logout in a second window
    delete logout_path
    follow_redirect!
    assert_select "a[href=?]", login_path
    assert_select "a[href=?]", logout_path,       count: 0
    assert_select "a[href=?]", user_path(@user),  count: 0
  end

  test "login with remembering" do
    log_in_as(@user, remember_me: '0')
    assert_nil cookies['remember_token']
  end

  test "login without remembering" do
    log_in_as(@user, remember_me: '0')
    assert_nil cookies['remember_token']
  end
end

1 个答案:

答案 0 :(得分:0)

错误#1:您需要提供更多详细信息。但是错误跟踪指示当您第二次使用delete方法调用logout_path时出现问题。你能提供你的sessions_helper.rb档案吗?

错误#2似乎来自index.html.erb模板。你可以发布代码吗?

错误#3:您尝试以@user身份登录,但未定义此实例变量。将行log_in_as(@user)更改为:log_in_as(@admin)

另外,请注意您的测试"login with remembering"是错误的,应该是:

test "login with remembering" do
  log_in_as(@user, remember_me: '1')
  assert_not_nil cookies['remember_token']
end