0
votes

I followed tutorial to write a test scenario: when users go to edit info page but not sign in yet, they will redirected to sign in page, and after they sign in, they will redirected to the edit page. But when i have done all like the tutorial, run the tests and fails, because wrong number of arguments (0 for 1) error.

This is error info:

AuthenticationPages authorization for non-signed-in users when attempting to visit a protected page after signing in should render the desired protected page
Failure/Error: click_button "Sign in"
ArgumentError: wrong number of arguments (0 for 1)
# ./app/helpers/sessions_helper.rb:31:in redirect_back_or'
# ./app/controllers/sessions_controller.rb:11:in
create'
# (eval):2:in click_button' # ./spec/requests/authentication_pages_spec.rb:58:inblock (5 levels) in..

This is my 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    
 .
 .
 .

In app/helpers/sessions_helper.rb, i have two methods, store_location and redirect_back_or to store and redirect back to url address which user visited before sign in:

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

Then i used store_location method in app/controllers/users_controller.rb:

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

And i used redirect_back_or method in app/controllers/sessions_controller.rb:

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

I don't know why this error happen, i think the redirect_back_or method already take argument user but it still error. Anybody can help me solve this? Thanks so much.

1

1 Answers

2
votes

Change this session.delete[:return_to] in your SessionsHelper for this session.delete(:return_to)