Rails 教程第 8 章测试期望重定向 return 200
Rails Tutorial Chapter 8 tests expect Redirect return 200
我看过类似标题的帖子,但它们没有与此完全相同的问题。
我正在编写 Rails 4 教程。我在第 8 章。我正在使用 Cloud9 的 IDE。当我在 运行 服务器上并直接登录网站时,一切正常。但是当我尝试 "login with valid information" 测试时,它应该变成绿色,但它没有。
我收到这个错误。
FAIL["test_login_with_valid_information", UsersLoginTest, 2015-05-10 20:30:40 +0000]
test_login_with_valid_information#UsersLoginTest (1431289840.31s)
Expected response to be a <redirect>, but was <200>
test/integration/users_login_test.rb:22:in `block in <class:UsersLoginTest>'
这让我觉得我用于测试的登录凭据有问题,但我不知道在哪里。
从 rails 控制台我得到以下信息:
>> User.first
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
=> #<User id: 1, name: "Michael Example", email: "michael@example.com", created_at: "2015-06-23 17:55:51", updated_at: "2015-06-23 18:52:37", password_digest: "a$Xl36B/1PmWJmsUOh3JBmnuQmhwLEIq1B5626jcW46fv...">
这是我的代码:
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' 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
test/fixtures/users.yml
michael:
name: Michael Example
email: michael@example.com
password_digest: <%= User.digest("password") %>
app/models/user.rb
class User < ActiveRecord::Base
before_save { email.downcase! }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
end
我完全无法解决这个问题。
根据请求添加:
app/controllers/sessions_controler.rb
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:passowrd])
log_in user
redirect_to user
else
flash.now[:danger] = 'Invalid email/password combination' #Not quite right!
render 'new'
end
end
def destroy
log_out
redirect_to root_url
end
end
谢谢 steve klein 帮我找到答案。问题出在会话控制器中。
if user && user.authenticate(params[:session][:passowrd])
一个简单的错字(密码)把我搞砸了。
我看过类似标题的帖子,但它们没有与此完全相同的问题。
我正在编写 Rails 4 教程。我在第 8 章。我正在使用 Cloud9 的 IDE。当我在 运行 服务器上并直接登录网站时,一切正常。但是当我尝试 "login with valid information" 测试时,它应该变成绿色,但它没有。
我收到这个错误。
FAIL["test_login_with_valid_information", UsersLoginTest, 2015-05-10 20:30:40 +0000]
test_login_with_valid_information#UsersLoginTest (1431289840.31s)
Expected response to be a <redirect>, but was <200>
test/integration/users_login_test.rb:22:in `block in <class:UsersLoginTest>'
这让我觉得我用于测试的登录凭据有问题,但我不知道在哪里。
从 rails 控制台我得到以下信息:
>> User.first
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
=> #<User id: 1, name: "Michael Example", email: "michael@example.com", created_at: "2015-06-23 17:55:51", updated_at: "2015-06-23 18:52:37", password_digest: "a$Xl36B/1PmWJmsUOh3JBmnuQmhwLEIq1B5626jcW46fv...">
这是我的代码:
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' 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
test/fixtures/users.yml
michael:
name: Michael Example
email: michael@example.com
password_digest: <%= User.digest("password") %>
app/models/user.rb
class User < ActiveRecord::Base
before_save { email.downcase! }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
end
我完全无法解决这个问题。
根据请求添加: app/controllers/sessions_controler.rb
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:passowrd])
log_in user
redirect_to user
else
flash.now[:danger] = 'Invalid email/password combination' #Not quite right!
render 'new'
end
end
def destroy
log_out
redirect_to root_url
end
end
谢谢 steve klein 帮我找到答案。问题出在会话控制器中。
if user && user.authenticate(params[:session][:passowrd])
一个简单的错字(密码)把我搞砸了。