Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6553669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:36:34+00:00 2026-05-25T12:36:34+00:00

I am trying to stub authentication for a controller in rspec. When I stub

  • 0

I am trying to stub authentication for a controller in rspec. When I stub the authorize method the test always passed no matter what the value I supply.

Controller:

class FoosController < ApplicationController
  before_filter :authorize
  ...
end

ApplicationController:

class ApplicationController < ActionController::Base
  protect_from_forgery

  helper_method :current_user

  protected

  def authorize
    return true if current_user

    flash[:error] = 'Please login'
    redirect_to signin_path
    false
  end

  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end
end

Specs:

# this passes (expected)
it "..." do
  controller.stubs(:current_user).returns(User.new)
  get :index
  response.should be_success
end

# this fails (expected)
it "..." do
  controller.stubs(:current_user).returns(nil)
  get :index
  response.should be_success
end

# this passes (expected)
it "..." do
  controller.stubs(:authorize).returns(true)
  get :index
  response.should be_success
end

# Problem: this passes (unexpected)
it "..." do
  controller.stubs(:authorize).returns(false)
  get :index
  response.should be_success
end

It seems like as soon as I stub :authorize, no matter what value is set, it always passes the before_filter. I thought it might be the protected/helper_method designations, but playing with those didn’t change anything.

Why does stubbing :authorize with false cause the before_filter to pass?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:36:35+00:00Added an answer on May 25, 2026 at 12:36 pm

    I think you need to check WHAT is being rendered.

    Looking at your code, if the call-chain does indeed stop when authorize returns false,
    then what is going to happen?

    There is no redirect, or render call.

    So it will be an empty response?

    An empty response would still be a 200.

    However, depending what version of Rails you’re using, its possible in Rails 3.1 that a before_filter returning false no longer stops the chain.

    Really, a before_filter that wants to stop the chain should do one of the following

    1. redirect somewhere
    2. render something
    3. raise something
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to stub a method that returns multiple values. For example: class
I'm trying to set up a stub with Rhino Mocks which returns a value
This is a bit of an odd one. I'm trying to stub a method
Benn trying to test a service layer method which returns a simple IList using
I am trying to test my controller and maintain separation of concerns. The first
I'm trying to test a method which calls a couple of other methods in
The method I am trying to test is: def self.load_file(file) lookup = '' if
Using RhinoMocks, I am trying to Stub the getter value of a property. The
I'm trying to test some legacy code, using Mockito. I want to stub a
I must be missing something. I'm trying to stub methods on a class in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.