繁体   English   中英

Rails教程-调试包exec rake测试

[英]Rails Tutorial - debugging bundle exec rake test

我是新用户,请不要杀死我。 我一直在学习Michael Hartl第9章的Rails教程,并取得了一些成功。 在运行“ 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:您需要提供更多详细信息。 但是错误跟踪表明,当您第二次使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM