Ruby on rails Rails教程第8章测试预期重定向返回200

Ruby on rails Rails教程第8章测试预期重定向返回200,ruby-on-rails,ruby,ruby-on-rails-4,railstutorial.org,Ruby On Rails,Ruby,Ruby On Rails 4,Railstutorial.org,我看过标题类似的线程,但它们没有与此完全相同的问题 我正在学习Rails 4教程。我在读第八章。我正在使用Cloud9的IDE。当我运行服务器并直接登录网站时,它工作正常。但是当我尝试使用有效信息登录测试时(应该显示为绿色),它不会 我得到这个错误 FAIL["test_login_with_valid_information", UsersLoginTest, 2015-05-10 20:30:40 +0000] test_login_with_valid_information#UsersL

我看过标题类似的线程,但它们没有与此完全相同的问题

我正在学习Rails 4教程。我在读第八章。我正在使用Cloud9的IDE。当我运行服务器并直接登录网站时,它工作正常。但是当我尝试使用有效信息登录测试时(应该显示为绿色),它不会

我得到这个错误

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: "$2a$10$Xl36B/1PmWJmsUOh3JBmnuQmhwLEIq1B5626jcW46fv...">
这是我的密码:

测试/集成/用户\u登录\u 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
测试/夹具/用户.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\u 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][: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
谢谢你帮我找到答案。问题出在会话控制器中

if user && user.authenticate(params[:session][:passowrd])

一个简单的输入密码把我搞砸了。

我们能看看你的会话控制器吗?它将帮助我们了解创建新会话时会发生什么。如果user&&user.authenticateparams[:session][:passowrd]现在正常工作,我在行中拼写了密码。