失败/错误:指定{expect(response).to redirect_to(root_url)}

时间:2016-10-09 11:49:06

标签: ruby-on-rails rspec rubygems rspec-rails rspec3

我无法理解,为什么不通过测试,它执行第一次重定向而不是第二次,如控制器代码本身所述,重定向完全按照描述进行。

Rspec 3.5.1

Rails 5

Ruby 2.3.1

规格/特征/ authentication_pages_spec.rb

describe "autorization", type: :request do

    describe "for non-signed-in users" do
        let(:user) { FactoryGirl.create(:user) }

        describe "in the Users controller" do

            describe "visiting the edit page" do
                before { visit edit_user_path(user) }
                it { should have_title('Log in') }
            end

            describe "submitting to the update action" do
                before { patch user_path(user) }
                specify { expect(response).to redirect_to(login_path) }
            end
        end
    end

    describe "as wrong user" do
        let(:user) { FactoryGirl.create(:user) }
        let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
        before { log_in user, no_capybara: true }

        describe "submitting a GET request to the Users#edit action" do
            before { get edit_user_path(wrong_user) }
            specify { expect(response.body).not_to match(full_title('Edit user')) }
            specify { expect(response).to redirect_to(root_url) }
        end

        describe "submitting a PATCH request to the User#update action" do
            before { patch user_path(wrong_user) }
            specify { expect(response).to redirect_to(root_url) }
        end
    end
end
end

测试失败,出现以下错误:

1)AuthenticationPages自动化作为错误的用户向用户#edit动作提交GET请求应该重定向到" http://www.example.com/"      失败/错误:指定{expect(response).to redirect_to(root_url)}

   Expected response to be a redirect to <http://www.example.com/> but was a redirect to <http://www.example.com/login>.
   Expected "http://www.example.com/" to be === "http://www.example.com/login".

2)AuthenticationPages自动化作为错误用户向User#update动作提交PATCH请求应该重定向到&#34; http://www.example.com/&#34;      失败/错误:指定{expect(response).to redirect_to(root_url)}

   Expected response to be a redirect to <http://www.example.com/> but was a redirect to <http://www.example.com/login>.
   Expected "http://www.example.com/" to be === "http://www.example.com/login".

我无法理解为什么当它重定向到根网址时它会重定向到登录网址 - 用户已在规范中登录。

这是UserController:

class UsersController < ApplicationController
  before_action :logged_in_user, only: [:edit, :update]
  before_action :correct_user,   only: [:edit, :update]


def show
    @user = User.find(params[:id])      
end

def new
    @user = User.new
end

def create
    @user = User.new(user_params)
    if @user.save
     log_in @user
     flash[:success] = "Welcome to the Sample App!"
     redirect_to @user
    else
     render 'new'   
  end
end

def edit
end

def update
  if @user.update_attributes(user_params)
    flash[:success] = "Profile updated"
    redirect_to @user
  else
    render 'edit'
  end
end

private

def user_params
    params.require(:user).permit(:name, :email, :password, :password_confirmation)
end

# Before filters

def logged_in_user
  unless logged_in?
    flash[:danger] = "Please log in."
    redirect_to login_url
  end
end

def correct_user
  @user = User.find(params[:id])
  redirect_to(root_url) unless current_user?(@user)
end
end

以下是我的sessions_helper.rb文件的相关位:

module SessionsHelper

# Logs in the given user.
def log_in(user)
  session[:user_id] = user.id
end

# Returns the current logged-in user (if any).
def current_user
  remember_token = User.encrypt(cookies[:remember_token])
  @current_user ||= User.find_by(id: session[:user_id])
end

def current_user?(user)
  user == current_user
end

# Returns true if the user is logged in, false otherwise.
def logged_in?
  !current_user.nil?
end

def log_out
    session.delete(:user_id)
    @current_user = nil
end
end

用户类:

class User < ApplicationRecord
  has_secure_password
  before_save { self.email = email.downcase }
  before_create :create_remember_token
  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+(\.[a-z]+)*\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX     },uniqueness: { case_sensitive: false }
  validates :password, length: { minimum: 6 }

  def User.new_remember_token
      SecureRandom.urlsafe_base64
  end

  def User.encrypt(token)
      Digest::SHA1.hexdigest(token.to_s)
  end

  private

  def create_remember_token
      self.remember_token = User.encrypt(User.new_remember_token)
  end
 end

规格/支持/ utilities.rb

include ApplicationHelper

def valid_login(user)
  fill_in "Email",    with: user.email
  fill_in "Password", with: user.password
  click_button "Log in"
end

RSpec::Matchers.define :have_error_message do |message|
  match do |page|
    expect(page).to have_selector('div.alert.alert-error', text:   message)
  end
end

def log_in(user, options={})
    if options[:no_capybara]
        # Sign in when not using Capybara.
        remember_token = User.new_remember_token
        cookies[:remember_token] = remember_token
        user.update_attribute(:remember_token,       User.encrypt(remember_token))
    else
        visit login_path
        fill_in "Email",        with: user.email
        fill_in "Password",     with: user.password
        click_button "Log in"   
    end
end

2 个答案:

答案 0 :(得分:1)

我怀疑你的问题与

有关
before { log_in user, no_capybara: true }

尝试将其更改为

before { session[:user_id] = user.id }

答案 1 :(得分:1)

问题在于你的log_in方法(在规范中,而不是帮助程序代码),它实际上并没有记录任何人。这就是你遇到登录错误的原因。您提到此代码“使用与默认值不同的电子邮件地址创建用户”,但这不是它应该做的事情;它应该是设置会话。

简单地创建用户不会设置会话,如果没有设置会话,logged_in?将为false,您将被重定向到登录页面。

尝试这样的事情:

before { @request.session[:user_id] = user.id }

这会明确根据用户ID的请求设置会话,以便logged_in?在您的控制器中返回true