Before_filter always running in test, not in development or production mode

I’m using Shoulda and restful_authentication on Rails3 and whenever I
run my tests a before filter for login_required is always triggered,
despite my filter being set up as:

before_filter :login_required, :only =>
[:update, :edit, :suspend, :unsuspend, :destroy, :purge]

My test is this:

context ‘A guest to the site’ do
context ‘on GET to :show’ do
setup { get :show, { :id => users(:alexander).id } }

  should_not_set_the_flash
  should_assign_to :user
  should_respond_with :success
end

end

The test fails though:

  1. Failure:
    test: A guest to the site on GET to :show should assign @user.
    (UsersControllerTest)
    []:
    Expected action to assign a value for @user

  2. Failure:
    test: A guest to the site on GET to :show should not set the flash.
    (UsersControllerTest)
    []:
    Did not expect the flash to be set, but was {:error=>“Sorry! You
    need to log in before visiting that page.”}

  3. Failure:
    test: A guest to the site on GET to :show should respond with
    success. (UsersControllerTest)
    []:
    Expected response to be a 200, but was 302

Here is the code for my before_filter that is actually making this
test fail:

def login_required
authorized? || access_denied
end

def access_denied
session[:return_to] = request.fullpath
flash[:error] = t(‘application.flash.error.login_required’)

redirect_to(new_sessions_path)

end

This code, as far as my understanding goes, should NOT ever be called
from this test though… I have the :only => [] in the controller to
not do it on show. Can anyone lend any insight as to why it is being
triggered? Another strange thing is this DOES NOT happen in production
or development modes using the browser. I can be logged out and visit
this page just fine without this before_filter being called.

This is apparently not related to Shoulda… This test also fails
using Test::Unit by itself:

test ‘A guest on GET to #show’ do
get :show, { :id => users(:alexander).id }

assert flash.empty?
assert_not_nil assigns(:user)
assert_response :success

end

Failure:

  1. Failure:
    test_A_guest_on_GET_to_#show(UsersControllerTest) [test/functional/
    users_controller_test.rb:44]:
    is not true.

Line 44 is the flash being empty, it is, once again set to:
{:error=>“Sorry! You need to log in before visiting that page.”}

Am I missing something obvious? Why is this filter always running? Is
this related to Rails3?

Thanks in advance.

-John P.

On May 27, 1:07 pm, John P. [email protected]

I found a way around this, though I still have no clue why this works:

In my controller, if I add:

def show
end

The before filter DOES NOT run, as I expect, as soon as I remove the
def show;end the filter is once again applied to the #show action. Is
this a Rails3 bug?

-John

On May 27, 3:27 pm, John P. [email protected]

John P. wrote in post #915199:

I found a way around this, though I still have no clue why this works:

In my controller, if I add:

def show
end

The before filter DOES NOT run, as I expect, as soon as I remove the
def show;end the filter is once again applied to the #show action. Is
this a Rails3 bug?

-John

On May 27, 3:27 pm, John P. [email protected]

This might be of interest:

https://rails.lighthouseapp.com/projects/8994/tickets/5673-rails3-conditional-before_filter-not-always-executed-for-implicit-resource-actions#ticket-5673-33