Advertisement
Guest User

Untitled

a guest
Jul 10th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. 1) Failure:
  2. UsersSignupTest#test_valid_signup_information [/home/ubuntu/workspace/test/integration/users_signup_test.rb:25]:
  3. expecting <"users/show"> but rendering with <["statics/home", "layouts/_shim", "layouts/_header", "layouts/_footer", "layouts/application"]>
  4.  
  5. 2) Error:
  6. UsersLoginTest#test_login_with_valid_information_followed_by_logout:
  7. NoMethodError: undefined method `[]' for nil:NilClass
  8. app/controllers/sessions_controller.rb:7:in `create'
  9. test/integration/user_login_test.rb:21:in `block in <class:UsersLoginTest>'
  10.  
  11. 3) Error:
  12. UsersLoginTest#test_login_with_invalid_information:
  13. NoMethodError: undefined method `[]' for nil:NilClass
  14. app/controllers/sessions_controller.rb:7:in `create'
  15. test/integration/user_login_test.rb:12:in `block in <class:UsersLoginTest>'
  16.  
  17. require 'test_helper'
  18.  
  19. class UsersSignupTest < ActionDispatch::IntegrationTest
  20.  
  21. test "invalid signup information" do
  22. get signup_path
  23. assert_no_difference 'User.count' do
  24. post users_path, user: { name: "",
  25. email: "user@invalid",
  26. password: "foo",
  27. password_confirmation: "bar" }
  28. end
  29. assert_template 'users/new'
  30. end
  31.  
  32. test "valid signup information" do
  33. get signup_path
  34. assert_difference 'User.count', 1 do
  35. post users_path, user: { name: "Example User",
  36. email: "user@stpauls.school.nz",
  37. password: "password",
  38. password_confirmation: "password" }
  39. end
  40. follow_redirect!
  41. assert_template 'users/show'
  42. assert is_logged_in?
  43. end
  44. end
  45.  
  46. require 'test_helper'
  47.  
  48. class UsersLoginTest < ActionDispatch::IntegrationTest
  49.  
  50. def setup
  51. @user = users(:someName)
  52. end
  53.  
  54. test "login with invalid information" do
  55. get login_path
  56. assert_template 'sessions/new'
  57. post login_path, params: { session: { email: "", password: "" } }
  58. assert_template 'sessions/new'
  59. assert_not flash.empty?
  60. get root_path
  61. assert flash.empty?
  62. end
  63.  
  64. test "login with valid information followed by logout" do
  65. get login_path
  66. post login_path, params: { session: { email: @user.email,
  67. password: 'password' } }
  68. assert is_logged_in?
  69. assert_redirected_to @user
  70. follow_redirect!
  71. assert_template 'users/show'
  72. assert_select "a[href=?]", login_path, count: 0
  73. assert_select "a[href=?]", logout_path
  74. assert_select "a[href=?]", user_path(@user)
  75. delete logout_path
  76. assert_not is_logged_in?
  77. assert_redirected_to root_url
  78. follow_redirect!
  79. assert_select "a[href=?]", login_path
  80. assert_select "a[href=?]", logout_path, count: 0
  81. assert_select "a[href=?]", user_path(@user), count: 0
  82. end
  83. end
  84.  
  85. class SessionsController < ApplicationController
  86.  
  87. def new
  88. end
  89.  
  90. def create
  91. user = User.find_by(email: params[:session][:email].downcase)
  92. if user && user.authenticate(params[:session][:password])
  93. log_in user
  94. redirect_to root_url
  95. else
  96. flash.now[:danger] = 'Invalid email/password combination'
  97. render 'new'
  98. end
  99. end
  100.  
  101. def destroy
  102. log_out
  103. flash.now[:success] = 'Successfully logged out!'
  104. redirect_to root_url
  105. end
  106. end
  107.  
  108. ENV['RAILS_ENV'] ||= 'test'
  109. require File.expand_path('../../config/environment', __FILE__)
  110. require 'rails/test_help'
  111.  
  112. class ActiveSupport::TestCase
  113. # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  114. fixtures :all
  115.  
  116. # Add more helper methods to be used by all tests here...
  117. def is_logged_in?
  118. !session[:user_id].nil?
  119. end
  120.  
  121. def log_in_as(user, options = {})
  122. password = options[:password] || 'example'
  123. end
  124. end
  125.  
  126. module SessionsHelper
  127.  
  128. # Logs in the given user.
  129. def log_in(user)
  130. session[:user_id] = user.id
  131. end
  132.  
  133. # Returns the current logged-in user (if any).
  134. def current_user
  135. @current_user ||= User.find_by(id: session[:user_id])
  136. end
  137.  
  138. # Returns true if the user is logged in, false otherwise.
  139. def logged_in?
  140. !current_user.nil?
  141. end
  142.  
  143. # Logs out the current user.
  144. def log_out
  145. session.delete(:user_id)
  146. @current_user = nil
  147. end
  148.  
  149. end
  150.  
  151. somename:
  152. name: "SomeName"
  153. email: "somename@someemail.com"
  154. password_digest: <%= User.digest('password') %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement