rubyonrails的rspecput方法不';看不到已登录用户

rubyonrails的rspecput方法不';看不到已登录用户,ruby,authentication,rspec,ruby-on-rails-3.2,Ruby,Authentication,Rspec,Ruby On Rails 3.2,因此,我已经使用迈克尔·哈特尔的教程一段时间了,我可以说它真的很有用,但有一个问题,我认为这不是教程的一部分。因此,在第9.2.2章“需要正确的用户”中,有一个测试,用于检查用户既不能访问其他用户的编辑页面,也不能提交直接提交请求 describe "as wrong user" do let(:user) { FactoryGirl.create(:user) } let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@ex

因此,我已经使用迈克尔·哈特尔的教程一段时间了,我可以说它真的很有用,但有一个问题,我认为这不是教程的一部分。因此,在第9.2.2章“需要正确的用户”中,有一个测试,用于检查用户既不能访问其他用户的编辑页面,也不能提交直接提交请求

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

  describe "visiting Users#edit page" do
    before { visit edit_user_path(wrong_user) }
    it { should_not have_selector('title', text: full_title('Edit user')) }
  end

  describe "submitting a PUT request to the Users#update action" do
    before { put user_path(wrong_user) }
    specify { response.should redirect_to(root_path) }
  end
end
这么长时间似乎一切正常,但测试失败:

1) Authentication authorization as wrong user submitting a PUT request to the Users#update action ←[31mFailure/Error:←[0m ←[31mspecify { response.should redirect_to(root_path }←[0m←[31mExpected response to be a redirect to <http://www.example.com/> but was a redirect to <http://www.example.com/signin>←[0m←[36m # ./spec/requests/authentication_pages_spec.rb:107:in `block (5 levels) in <top (required)>'←[0m
1)身份验证授权为向用户提交PUT请求的错误用户#更新操作←[31M故障/错误:←[0m←[31mspecify{response.should将_重定向到(根路径)}←[0m←[31M预期响应是重定向到的,但却是重定向到的←[0m←[36m./spec/requests/authentication_pages_spec.rb:107:in'block(5层)in'←[0m
以下是用户控制器:

class UsersController < ApplicationController
before_filter :signed_in_user, only: [:index, :edit, :update]
before_filter :correct_user,   only: [:edit, :update]

def index
  @users = User.all
end

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

def new
@user = User.new
end

def create
  @user = User.new(params[:user])
  if @user.save
    sign_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(params[:user])
    flash[:success] = "Profile updated"
    sign_in @user
    redirect_to @user
  else
    render 'edit'
  end
end

private

  def signed_in_user
    unless signed_in?
      puts "No user signed in"
    store_location
      redirect_to signin_path, notice: "Please sign in."
    end
  end

  def correct_user
    @user = User.find(params[:id])
  puts "Incorrect user" unless current_user?(@user)
    redirect_to(root_path) unless current_user?(@user)
  end
end
class UsersController
因此,正如您所看到的,问题在于,当使用rspecput方法时,测试甚至在检查正确的用户之前就失败了,因为它看到没有用户登录。
这是一个很容易忽略的小问题(不正确的用户无论如何都不能发出直接PUT请求)但这对我来说是一个谜,为什么它不能正确工作,我已经有很长一段时间不能得到答案。

看起来
登录用户
过滤器在
正确用户
触发之前重定向回登录页面。这表明用户实际上没有被
登录用户
ca正确登录我在前面的街区

您是否在
spec/support/utilities.rb
中定义了登录

include ApplicationHelper

def sign_in(user)
  visit signin_path
  fill_in "Email",    with: user.email
  fill_in "Password", with: user.password
  click_button "Sign in"
  # Sign in when not using Capybara as well.
  cookies[:remember_token] = user.remember_token
end

谢谢,Steve,但是登录已定义,因此似乎不是这样。但似乎登录失败。您可以尝试从测试中调用
save\u和\u open\u form
,查看页面上是否有任何错误消息。或者,log/test.log中是否有任何线索?