【发布时间】:2016-04-26 19:00:46
【问题描述】:
我是新用户,所以请不要杀了我。我一直在阅读 Michael Hartl 的 Rails 教程第 9 章,并取得了一些成功。在运行“bundle exec rake test”命令时,我遇到了 3 个错误。这些不会影响测试的状态,因此根据 Hartl 先生的规范,我的测试仍然是红色或绿色,但我的网页在 Web 上的外观存在一些问题,我担心错误可能会滚雪球。
有人知道这里发生了什么吗?我已经尽我所能在 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/models/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
【问题讨论】:
-
我们需要查看一些代码来帮助您。
-
你能添加测试吗?
-
谢谢。我的源代码基本上是书中的逐字代码。我已经抽查了错字。
-
我已经发布了源代码:测试错误。感谢您提供帮助!
标签: ruby-on-rails railstutorial.org