RoR教程第9章(第9.2.3节)测试失败,因为参数数量错误(0表示1)

时间:2012-09-05 18:34:15

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1 rspec-rails

我按照教程编写测试场景:当用户转到编辑信息页面但尚未登录时,他们将重定向到登录页面,登录后,他们将重定向到编辑页面。但是当我完成所有这些教程时,运行测试并失败,因为wrong number of arguments (0 for 1)错误。

这是错误信息:

  

在登录后尝试访问受保护页面时,对非登录用户的AuthenticationPages授权应呈现所需的受保护页面
       失败/错误:click_button“登录”
       引发ArgumentError:          错误的参数数量(0表示1)
       #./app/helpers/sessions_helper.rb:31:in redirect_back_or'
# ./app/controllers/sessions_controller.rb:11:in
创建'
       #(eval):2:在click_button' # ./spec/requests/authentication_pages_spec.rb:58:in块(5级)中..

这是我的spec / requests / authentication_pages_spec.rb

describe "authorization" do

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

    describe "when attempting to visit a protected page" do
      before do
        visit edit_user_path(user)
        fill_in "Email", with: user.email
        fill_in "Password", with: user.password
        click_button "Sign in"
      end

      describe "after signing in" do

        it "should render the desired protected page" do
          page.should have_selector('title', text: 'Edit user')
        end
      end
    end    
 .
 .
 .

在app / helpers / sessions_helper.rb中,我有两个方法store_locationredirect_back_or来存储并重定向回用户在登录前访问过的网址:

module SessionsHelper

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

  def store_location
    session[:return_to] = request.url
  end
end

然后我在app / controllers / users_controller.rb中使用了store_location方法:

class UsersController < ApplicationController

  before_filter :signed_in_user, only: [:edit, :update]
  before_filter :correct_user, only: [:edit, :update]
  .
  .
  private

    def signed_in_user
      unless signed_in?
        store_location
        redirect_to signin_url, notice: "Please sign in."
      end
    end

我在app / controllers / sessions_controller.rb中使用了redirect_back_or方法:

class SessionsController < ApplicationController

  def new

  end

  def create
    user = User.find_by_email(params[:email].downcase)
    if user && user.authenticate(params[:password])
      sign_in user
      redirect_back_or user
    else
      flash.now[:error] = "Invalid email/password combination"
      render 'new'
    end
  end

我不知道为什么会发生这种错误,我认为redirect_back_or方法已经采用了参数user,但它仍然是错误的。有人可以帮我解决这个问题吗?非常感谢。

1 个答案:

答案 0 :(得分:2)

更改此session.delete[:return_to]

的SessionsHelper中的session.delete(:return_to)