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:
-
Failure:
test: A guest to the site on GET to :show should assign @user.
(UsersControllerTest)
[]:
Expected action to assign a value for @user -
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.”} -
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.