Test errors in fuctional test after adding before_filter :login_required to controller

Hello,

I added the “before_filter” to my controllers to require a login of the
user.

Here’s an example of my Unit Controller with the added before_filter:

IN THE ATTACHED FILE

When executing the tests with rake test, I get different error messages.

To show you my errors, I only executed the unit controller test with the
following line:
ruby -Itest test/functional/units_controller_test.rb

I get the folowing errors:

IN THE ATTACHED FILE

If I delete the “before_filter :login_required” line from the unit
controller, the test will be executed without any errors.

I have a controller_authentication.rb file within my /lib directory. The
content of the file:

IN THE ATTACHED FILE

Can I use one of the methods to get access for the tests?

I tried to find a solution for that problem, but I could not find any
during the last two days.
Please give me advice to solve that problem.

Thanks in advance,
Andre

Phil

2011/3/30 Andre S. [email protected]

You’ll need to create a user and make sure they are logged in, within
any of
your tests that require the user to be logged in. How you do this
depends on
you’re authenticating your users…

In your tests, you’ll need to log in a user otherwise the before_filter
in your controller redirects to the login page.

I suggest you change a portion of your authentication code, in
particular

def logged_in?
# current_user
session[:user_id]
end

So instead of calling current_user, which hits the database to fetch the
User by the way, you can just return session[:user_id]. This is good
enough to figure out if a user is logged in as it will return nil if the
key
:user_id is not set, and nil is as good as false in ruby.

With that change, you can then set a session variable in your functional
test, eg

class UnitsControllerTest < ActionController::TestCase
def test_index
get :index, { }, {‘user_id’ => 1 }
assert_template ‘index’
end

The first hash after the action is for parameters, and the second one is
for
session variables.

Ideally though, you should be testing different scenarios to see what
the
behavior will be when a user is logged in or not.

I highly recommend that you look at shoulda and flexmock for testing.
Shoulda allows you to easily nest contexts for your tests while flexmock
allows you to set up mocks to avoid hitting the database or setting up
session variables as done above.

Instead, you can have something like this in your test_helper.rb

def be_logged_in
flexmock(@controller).should_receive(:logged_in?).and_return(true)
end

def be_logged_out
flexmock(@controller).should_receive(:logged_in?).and_return(false)
end

Then in your shoulda setup blocks, you just call one of those two
helpers
depending on the context you are testing.

Have fun,
Franz

I solved my problem by using fixtures. The tests run without any
errors. I wrote a short article at:

http://www.faustas.de/rails-test-error-beforefilter-loginrequired-controller