测试页面重定向的问题

时间:2016-09-05 18:32:03

标签: ruby-on-rails ruby rspec

我一直在研究基于Micheal Hartl的教程书的Ruby on Rails,并且遇到了一个非常有趣的问题,如果我的网站中的某个用户尝试编辑另一个用户的配置文件,那么重定向到根页面的rspec测试适当的网址。问题是,当我启动rails服务器并手动检查此操作时,它实际上将我重定向到根URL,但是,规范测试显示:

Failure/Error: specify { 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/signin>.
   Expected "http://www.example.com/" to be === "http://www.example.com/signin".
 # ./spec/requests/authentication_pages_spec.rb:95:in `block (5 levels) in <top (required)>'

我搜索了网络以找到解决此问题的方法,并发现实际上有几个类似的问题,人们建议在no_capybara: true文件中使用rb,但是,我已经使用此解决方案。此测试的代码部分如下:

describe "as wrong user" do
  let(:user) { FactoryGirl.create(:user) }
  let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
  before { sign_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 Users#update action" do
    before { patch user_path(wrong_user) }
    specify { expect(response).to redirect_to(root_url) }
  end
end

控制器代码也在这里:

class UsersController < ApplicationController
  before_action :signed_in_user, only: [:index, :edit, :update]
  before_action :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(user_params)
    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(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 signed_in_user
      unless signed_in?
        store_location
        redirect_to signin_url, notice: "Please sign in."
      end
    end

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

以下是控制器的会话助手代码:

def current_user?(user)
    user == current_user
end

def sign_out
    current_user.update_attribute(:remember_token, User.encrypt(User.new_remember_token))
    cookies.delete(:remember_token)
    self.current_user = nil
end

def redirect_back_or(default)
    redirect_to(session[:return_to] || default)
    session.delete(:return_to)
end

我会很高兴得到任何答案,并随时准备提供任何细节!

1 个答案:

答案 0 :(得分:1)

似乎用户的doc { _id: 57c760de1e08000300525775, updatedAt: Wed Sep 07 2016 03:32:29 GMT-0400 (Eastern Daylight Time), createdAt: Wed Aug 31 2016 18:57:34 GMT-0400 (Eastern Daylight Time), vote: 'up', reviewText: 'this sisisfrfr', company: 57c4f982a82a799432999b63, companyName: 'comp1', userType: 'anon', user: { _id: 57c760cf1e08000300525774, __v: 1, usefulness: [], reviews: [ 57c760de1e08000300525775 ] }, __v: 2, className: '', createdString: 'We, August 31st 16, 6:57:34 pm', momented: '6 days ago', updatedString: 'We, September 7th 16, 3:32:23 am', userWhoVoted: [ { nowNotUseful: 0, nowUseful: 1, userWhoVotedId: 57cfc275e3ec2c3826ea55a0, result: 'notUseful' } ], usefulness: { usefulResult: 1, notUseful: 0, useful: 1 }, statements: [ { name: 'statement2', question: 'This is the question for statement2', result: 6 }, { name: 'statement3', question: 'This is the question for statement3', result: 9 } ] } 无效。请先检查用户是否真的通过debugger / byebug登录,或者只是编写一个小测试用例来验证sign_in(它不应该重定向)。

您可能还想检查您的测试get edit_user_path(user)方法是否正确实施:Can't understand no_capybara option in michael hart'l tutorial test。这也是为什么必须在这里使用sign_in选项的解释。