I'm a new user, so please don't kill me. I have been working through the Rails Tutorial by Michael Hartl, Chapter 9, with some success. In running the "bundle exec rake test" commands, I have encountered 3 errors. These do not affect the status of the tests, so my tests are still red or green per Mr. Hartl's specifications, but there are some issues with the appearance of my pages on the web, and I'm concerned that the errors might snowball.
Does anyone know what seems to be going on here? I've hand keyed his code blocks in the Cloud9 IDE to the best of my ability, so the syntax should be correct with 100% accuracy. My background is Java and C#, and these remind me of compiler errors.
1) Error:
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) Error:
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) Error:
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 %>
test/integration/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
test/integration/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
Error #1: you need to provide more details. But the error trace indicates that something goes wrong when you call the logout_path with the delete method for the second time. Can you provide your sessions_helper.rb
file ?
Error #2 seems to come from the index.html.erb
template. Can you post the code?
Error #3: you are trying to log in as @user
, but this instance variable is not defined. Change the line log_in_as(@user)
to: log_in_as(@admin)
.
Also, take care that your test "login with remembering"
is wrong, it should be:
test "login with remembering" do
log_in_as(@user, remember_me: '1')
assert_not_nil cookies['remember_token']
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.