I've just gone thru chapter 8 of the Hartl tutorial but keep getting an error when I try to run the tests. I'm getting 3 of the same error and the issue seems to stem from the user login test, where fixture :michael is not being found in the fixture set (users).
Here is my user_login_test
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" do
get login_path
post login_path, session: { email: #user.email, password: 'password' }
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)
end
end
Here is the error
ERROR["test_login_with_invalid_information", UsersLoginTest, 0.006789]
test_login_with_invalid_information#UsersLoginTest (0.01s)
StandardError: StandardError: No fixture named 'michael' found for fixture set 'users'
test/integration/users_login_test.rb:6:in `setup'
test/integration/users_login_test.rb:6:in `setup'
ERROR["test_login_with_valid_information", UsersLoginTest, 0.008174]
test_login_with_valid_information#UsersLoginTest (0.01s)
StandardError: StandardError: No fixture named 'michael' found for fixture set 'users'
test/integration/users_login_test.rb:6:in `setup'
test/integration/users_login_test.rb:6:in `setup'
DEPRECATION WARNING: The assertion was not run because of an invalid css selector.
unexpected '#' after '[#<Nokogiri::CSS::Node:0x007fa642d4e870 #type=:ELEMENT_NAME, #value=["div"]>]' (called from block in <class:UsersSignupTest> at /Users/SamDavidoff/Documents/workspace/sample_app/test/integration/users_signup_test.rb:14)
DEPRECATION WARNING: The assertion was not run because of an invalid css selector.
unexpected '<' after '.' (called from block in <class:UsersSignupTest> at /Users/SamDavidoff/Documents/workspace/sample_app/test/integration/users_signup_test.rb:15)
ERROR["test_valid_signup_information", UsersSignupTest, 0.197435]
test_valid_signup_information#UsersSignupTest (0.20s)
NoMethodError: NoMethodError: undefined method `is_logged_in?' for #<UsersSignupTest:0x007fa642dd6068>
test/integration/users_signup_test.rb:27:in `block in <class:UsersSignupTest>'
test/integration/users_signup_test.rb:27:in `block in <class:UsersSignupTest>'
You didn't create user in file test/fixtures/users.yml
Related
I'm new on StackOverflow as I've recently begun to learn Ruby on Rails with Michael Hartl's Tutorial. I can't seem to pass the test of listing 8.25 ("At this point, the test suite should still be GREEN:" $ bundle exec rake test):
My test is RED and the error is as follows:
~/workspace/sample_app (log-in-log-out) $ bundle exec rake test
Started
ERROR["test_layout_links", SiteLayoutTest, 2016-04-14 23:15:47 +0000]
test_layout_links#SiteLayoutTest (1460675747.92s)
NoMethodError: NoMethodError: undefined method `full_title' for # <SiteLayoutTest:0x0000000a30bad8>
test/integration/site_layout_test.rb:13:in `block in <class:SiteLayoutTest>'
test/integration/site_layout_test.rb:13:in `block in <class:SiteLayoutTest>'
22/22: [======================================================] 100% Time: 00:00:01, Time: 00:00:01
Finished in 1.30261s
tests, 49 assertions, 0 failures, 1 errors, 0 skips
I've gone through all the recent changes in chapter 7 and 8 again; the tests of 8.20, 8.21 and 8.24 just pass.
The file site_layout_test mentioned in the error looks like this:
require 'test_helper'
class SiteLayoutTest < ActionDispatch::IntegrationTest
test "layout links" do
get root_path
assert_template 'static_pages/home'
assert_select "a[href=?]", root_path, count: 2
assert_select "a[href=?]", help_path
assert_select "a[href=?]", about_path
assert_select "a[href=?]", contact_path
get signup_path
assert_select "title", full_title("Sign up")
end
end
ADDITIONS:
1) This is the tutorial I'm doing: https://www.railstutorial.org/book/log_in_log_out
2) This is the file that the error refers to:
require 'test_helper'
class SiteLayoutTest < ActionDispatch::IntegrationTest
test "layout links" do
get root_path
assert_template 'static_pages/home'
assert_select "a[href=?]", root_path, count: 2
assert_select "a[href=?]", help_path
assert_select "a[href=?]", about_path
assert_select "a[href=?]", contact_path
get signup_path
assert_select "title", full_title("Sign up")
end
end
If I take the last assert_select out, I don't get the error anymore.
And this is the application_helper:
module ApplicationHelper
# Returns the full title on a per-page basis.
def full_title(page_title = '')
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
page_title + " | " + base_title
end
end
end
Your help is much appreciated. Thanks in advance!
I would wager the reason is because you haven't performed the step in Listing 5.37, where you include in the ApplicationHelper in your test_helper.rb:
class ActiveSupport::TestCase
fixtures :all
include ApplicationHelper
.
.
.
end
On chapter 9.2 of Hartl's Ruby on Rails Tutorial, I'm getting this 2 errors on the rake test after i made the test for redirecting user when they try to edit or update a profile that isn't theirs :
ERROR["test_should_redirect_edit_when_logged_in_as_wrong_user", UsersControllerTest, 2016-04-18 06:27:31 +0100]
test_should_redirect_edit_when_logged_in_as_wrong_user#UsersControllerTest (1460957251.39s)
NoMethodError: NoMethodError: undefined method `user_id' for #<User:0x00564114110958>
test/test_helper.rb:24:in `log_in_as'
test/controllers/users_controller_test.rb:28:in `block in <class:UsersControllerTest>'
test/test_helper.rb:24:in `log_in_as'
test/controllers/users_controller_test.rb:28:in `block in <class:UsersControllerTest>'
ERROR["test_should_redirect_update_when_logged_in_as_wrong_user", UsersControllerTest, 2016-04-18 06:27:31 +0100]
test_should_redirect_update_when_logged_in_as_wrong_user#UsersControllerTest (1460957251.40s)
NoMethodError: NoMethodError: undefined method `user_id' for #<User:0x00564114035628>
test/test_helper.rb:24:in `log_in_as'
test/controllers/users_controller_test.rb:36:in `block in <class:UsersControllerTest>'
test/test_helper.rb:24:in `log_in_as'
test/controllers/users_controller_test.rb:36:in `block in <class:UsersControllerTest>'
The errors messages points to line 24 in my test_helper and line 28,36 in my users_controller_test :
test/test_helper.rb:24:in `log_in_as'
test/controllers/users_controller_test.rb:28:in `block in <class:UsersControllerTest>'
test/test_helper.rb:24:in `log_in_as'
test/controllers/users_controller_test.rb:28:in `block in <class:UsersControllerTest>'
test/test_helper.rb:24:in `log_in_as'
test/controllers/users_controller_test.rb:36:in `block in <class:UsersControllerTest>'
test/test_helper.rb:24:in `log_in_as'
test/controllers/users_controller_test.rb:36:in `block in <class:UsersControllerTest>'
test_helper.rb :
Line 18 would be at "remember_me" in the "log_in_as" method.
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use!
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical
# order.
fixtures :all
# Returns true if a test user is logged in.
def is_logged_in?
!session[:user_id].nil?
end
# Log in a test user
def log_in_as(user, options = {})
password = options[:password] || 'password'
remember_me = options[:remember_me] || '1'
if integration_test?
post login_path, session: {email: user.email,
password: password,
remember_me: remember_me}
else
session[:user_id] = user.user_id
end
end
# Returns true inside an integration test.
def integration_test?
defined?(post_via_redirect)
end
# Add more helper methods to be used by all tests here...
end
user_controller_test.rb :
Line 28 would be at "log_in_as(#other_user)" in test "should redirect update when not logged in" and 36 at "log_in_as(#other_user)" in test "should redirect update when logged in as wrong user".
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
def setup
#user = users(:michael)
#other_user = users(:archer)
end
test "should get new" do
get :new
assert_response :success
end
test "should redirect edit when not logged in" do
get :edit, id: #user
assert_not flash.empty?
assert_redirected_to login_url
end
test "should redirect update when not logged in" do
patch :update, id: #user, user: { name: #user.name, email: #user.email }
assert_not flash.empty?
assert_redirected_to login_url
end
test "should redirect edit when logged in as wrong user" do
log_in_as(#other_user)
get :edit, id: #user
assert flash.empty?
assert_redirected_to root_url
end
test "should redirect update when logged in as wrong user" do
log_in_as(#other_user)
patch :update, id: #user, user: { name: #user.name, email: #user.email }
assert flash.empty?
assert_redirected_to root_url
end
end
and this is the fixture file where :archer and :michael are stored(test/fixtures/users.yml):
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
michael:
name: Michael Example
email: michael#example.com
password_digest: <%= User.digest('password') %>
archer:
name: Sterling Archer
email: duchess#example.gov
password_digest: <%= User.digest('password') %>
else
session[:user_id] = user.user_id
end
There's no such attribute as user_id, you want...
else
session[:user_id] = user.id
end
Working through the Rails Tutorial and in chapter 8 I can't get one of my tests to pass. Been checking around for hours... any help would be greatly appreciated.
ERROR["test_login_with_valid_information", UsersLoginTest, 2015-09-07 00:25:37 -0700]
test_login_with_valid_information#UsersLoginTest (1441610737.31s)
BCrypt::Errors::InvalidHash:
BCrypt::Errors::InvalidHash: invalid hash
app/controllers/sessions_controller.rb:8:in `create'
test/integration/users_login_test.rb:22:in `block in <class:UsersLoginTest>'
app/controllers/sessions_controller.rb:8:in `create'
test/integration/users_login_test.rb:22:in `block in <class:UsersLoginTest>'
22/22: [============] 100% Time: 00:00:00, Time: 00:00:00
Finished in 0.46288s
22 tests, 44 assertions, 0 failures, 1 errors, 0 skips
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" do
get login_path
post login_path, session: { email: #user.email, password: 'password' }
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)
end
end
sessions_controller.rb
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
log_in user
redirect_to user
else
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
end
end
I know it's been a while since originally posted, but I had exactly the same error and my problem was a typo (missing =) in
users.yml
michael:
name: Michael Example
email: michael#example.com
password_digest: <% User.digest('password') %>
where it should have been
michael:
name: Michael Example
email: michael#example.com
password_digest: <%= User.digest('password') %>
#PraveenGeorge is correct - you need to back track and review your code at least since the last time the tests passed. Based on the fact that the content of the two files you've shared match to the tutorial, the problem lies elsewhere (and, unfortunately, out of sight from us on SO).
Perhaps it's in user.rb where you def User.digest(string) or in sessions_helper.rb?
Dear #Travislogan : As the ruby on rails tutorial itself is self explanatory in most possible way, you might have missed some of the steps/code before you reached into the section 8.2. So please cross check all tests and your project code from at least the previous chapter to the current one , one more time.It will help you to find out the error in your code.
I tried fixing it but to no avail. I'm pretty sure I followed the tutorial. This happened in chapter 8 of The Rails Tutorial.
Here is the error when i run bundle exec rake test
ERROR["test_current_user_returns_nil_when_remember_digest_is_wrong", SessionsHelperTest, 2015-09-17 16:33:02 +0100]
test_current_user_returns_nil_when_remember_digest_is_wrong#SessionsHelperTest (1442503982.53s)
NoMethodError: NoMethodError: undefined method `user' for #<SessionsHelperTest:0xbdef3ba8>
test/helpers/sessions_helper_test.rb:6:in `setup'
test/helpers/sessions_helper_test.rb:6:in `setup'
ERROR["test_current_user_returns_right_user_when_session_is_nil", SessionsHelperTest, 2015-09-17 16:33:02 +0100]
test_current_user_returns_right_user_when_session_is_nil#SessionsHelperTest (1442503982.55s)
NoMethodError: NoMethodError: undefined method `user' for #<SessionsHelperTest:0xb9575274>
test/helpers/sessions_helper_test.rb:6:in `setup'
test/helpers/sessions_helper_test.rb:6:in `setup'
ERROR["test_login_with_remembering", UsersLoginTest, 2015-09-17 16:33:02 +0100]
test_login_with_remembering#UsersLoginTest (1442503982.61s)
NoMethodError: NoMethodError: undefined method `session' for nil:NilClass
test/test_helper.rb:27:in `log_in_as'
test/integration/users_login_test.rb:51:in `block in <class:UsersLoginTest>'
test/test_helper.rb:27:in `log_in_as'
test/integration/users_login_test.rb:51:in `block in <class:UsersLoginTest>'
ERROR["test_login_without_remembering", UsersLoginTest, 2015-09-17 16:33:02 +0100]
test_login_without_remembering#UsersLoginTest (1442503982.73s)
NoMethodError: NoMethodError: undefined method `session' for nil:NilClass
test/test_helper.rb:27:in `log_in_as'
test/integration/users_login_test.rb:56:in `block in <class:UsersLoginTest>'
test/test_helper.rb:27:in `log_in_as'
test/integration/users_login_test.rb:56:in `block in <class:UsersLoginTest>'
28/28: [=======================================================================================================] 100% Time: 00:00:03, Time: 00:00:03
Finished in 3.29948s
28 tests, 62 assertions, 0 failures, 4 errors, 0 skips
test/test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use!
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
# Returns true if a test user is logged in.
def is_logged_in?
!session[:user_id].nil?
end
# Logs in a test user.
def log_in_as(user, options = {})
password = options[:password] || 'password'
remember_me = options[:remember_me] || '1'
if integration_test?
post login_path, session: { email: user.email,
password: password,
remember_me: remember_me }
else
session[:user_id] = user.user_id # this is line 27
end
end
private
# Returns true inside an integration test.
def integration_test?
defined?(post_via_reditect)
end
end
test/helpers/sessions_helper.rb
require 'test_helper'
class SessionsHelperTest < ActionView::TestCase
def setup
#user = user(:microte) # Line 6
remember(#user)
end
test "current_user returns right user when session is nil" do
assert_equal #user, current_user
assert is_logged_in?
end
test "current_user returns nil when remember digest is wrong" do
#user.update_attribute(:remember_digest, User.digest(User.new_token))
assert_nil current_user
end
end
test/integration/users_login_test.rb
require 'test_helper'
class UsersLoginTest < ActionDispatch::IntegrationTest
def setup
#user = users(:microte)
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" do
get login_path
post login_path, session: { email: #user.email, password: 'password' }
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)
end
test "login with valid information follwed 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: '1') # Line 51
assert_not_nil cookies['remember_token']
end
test "login without remembering" do
log_in_as(#user, remember_me: '0') # Line 56
assert_nil cookies['remember_token']
end
end
Thanks for your help.
It looks like the first two errors are in your session_helper: your setup defines #user = user(:microte).
Not sure about the errors in the remembering tests are; the tests you've shared appear to be fine. I suggest you compare the code changes against what's in the tutorial to see what the cause of these errors are.
Seems like you are getting an error when trying to pull out a user from your fixtures. Check your fixtures/users.yml file and see if you have a microte user defined. It should look something like this(just an example):
microte:
name: "Test User"
email: "test_user#example.com"
password_digest: <%= User.digest("password")%>
slug: <%= "Test User".parameterize %>
activated: true
In the tutorial the user provided is named :michael
As others before me have noted, you wrote :microte instead.
Check in your users.yml fixture how you named him there, and make sure they match.
As a bit of explanation: in the beginning of your test there is a setup method that prepares your test setting:
def setup
#user = users(:microte)
end
the user you are instantiating here, is put into the fixtures file.
FAIL["test_test_login_with_valid_information", UsersLoginTest, 2015-08-27 20:00:02 +0000]
test_test_login_with_valid_information#UsersLoginTest (1440705602.54s)
Expected at least 1 element matching "a[href="/users.762146111"]", found 0..
Expected 0 to be >= 1.
test/integration/users_login_test.rb:18:in `block in '
2/2:
[=======================================================================================================] 100% Time: 00:00:00, Time: 00:00:00
Finished in 0.46825s 2 tests, 10 assertions, 1 failures, 0 errors, 0
skips
Here's user_login_test.rb
require 'test_helper'
class UsersLoginTest < ActionDispatch::IntegrationTest
def setup
#user = users(:michael)
end
test "test login with valid information" do
get login_path
post login_path, session: {email: #user.email, password: "password"}
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=?]", users_path(#user)
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 flash.any?
get root_path
assert flash.empty?
end
end
User.yml
michael:
name: michael
email: michael#example.com
password_digest: <%= User.digest("password") %>
I'd double-check your header template and confirm that the appropriate code is there for the "Profile" link (listing 8.16) as that appears what's failing the test.
I'm not super savvy with Rails, but this part of the error has me a little concerned: matching "a[href="/users.762146111"]". I'd expect that to be /users/762….