Ruby on rails Rails教程-调试bundle exec rake测试

Ruby on rails Rails教程-调试bundle exec rake测试,ruby-on-rails,railstutorial.org,Ruby On Rails,Railstutorial.org,我是新用户,所以请不要杀我。我一直在学习MichaelHartl的Rails教程第9章,并取得了一些成功。在运行“bundle exec rake test”命令时,我遇到了3个错误。这些不会影响测试的状态,因此根据Hartl先生的规范,我的测试仍然是红色或绿色的,但我的网页在web上的外观存在一些问题,我担心错误可能会滚雪球 有人知道这里发生了什么吗?我已经尽我所能在Cloud9IDE中手动键入了他的代码块,因此语法应该是100%正确的。我的背景是Java和C#,这让我想起了编译器错误 1)错

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

有人知道这里发生了什么吗?我已经尽我所能在Cloud9IDE中手动键入了他的代码块,因此语法应该是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>'
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>'
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
UsersLoginTest#测试(登录)(带有有效信息)(后跟)注销(logout:NoMethodError):未定义nil:NilClass的方法`忘记'
app/helpers/sessions\u helper.rb:41:在“忘记”中
app/helpers/sessions\u helper.rb:48:in'log\u out'
app/controllers/sessions\u controller.rb:18:in'destroy'
test/integration/users\u login\u test.rb:33:in'block in'
2)错误:

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>'
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>'
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
UsersIndexTest#test_index_as_a_non-admin:SyntaxError:/home/ubuntu/workspace/sample_app/app/views/users/index.html.erb:12:语法错误,意外关键字_sure,预期输入结束
test/integration/users\u index\u test.rb:31:in'block in'
3)错误:

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>'
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>'
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
UsersIndexTest#test_index_as_admin_包括分页和删除链接:
NoMethodError:nil:NilClass的未定义方法“email”
test/test\u helper.rb:19:in'log\u in\u as'
test/integration/users\u index\u test.rb:11:in'block in'
37次运行,84次断言,0次失败,3次错误,0次跳过
test/models/test\u helper.rb

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

<h1>All users</h1>

<%= will_paginate %>

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

<%= will_paginate %>
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
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

所有用户
test/integration/users\u index\u test.rb

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

<h1>All users</h1>

<%= will_paginate %>

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

<%= will_paginate %>
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
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
需要“测试助手”
类UsersIndexTest
test/integration/users\u login\u test.rb

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

<h1>All users</h1>

<%= will_paginate %>

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

<%= will_paginate %>
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
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
需要“测试助手”
类UsersLoginTest
错误1:您需要提供更多详细信息。但是错误跟踪表明,当您第二次使用delete方法调用logout_路径时,出现了一些错误。你能提供你的
sessions\u helper.rb
文件吗

错误2似乎来自
index.html.erb
模板。你能把密码寄出去吗

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

另外,请注意您的测试
“记住登录”
是错误的,应该是:

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

我们需要查看一些代码以帮助您。您可以添加测试吗?谢谢。我的源代码基本上是一字不差地从书中取出的代码。我已经抽查了打字错误。我已经发布了源代码:测试错误。谢谢你主动提供帮助!