Making functional tests in Rails with Devise

allesklar picture allesklar · Jul 6, 2010 · Viewed 11.2k times · Source

After 3 years of procrastination today is the day that I start testing my Rails apps. My first step is to fix the failing tests in my Rails 3 beta4 app.

My last 3 failing tests have to do with the devise gem and its authenticate_user! method in a before_filter at the top of my controller.

You'd earn great karma by helping me out with this since it will enable me to use the TDD methodology from now on.

Here is the error that troubles me:

1) Error:
test_should_get_accepted(ModerationControllerTest):
NoMethodError: undefined method `authenticate!' for nil:NilClass
    /test/functional/moderation_controller_test.rb:10:in `test_should_get_accepted'

Devise just gives functional tests pointers and helpers in this page: http://github.com/plataformatec/devise but I just don't know how to put this into application.

Can you please give this testing noob some detailed instructions on how to use these helpers?

Answer

allesklar picture allesklar · Jul 7, 2010

It took me a while but I found the way. Here it is for anyone stuck at the same point:

At the top of the moderation_controller_test.rb, below the class declaration, add this line:

include Devise::TestHelpers

I have 2 records in my user fixture and I added this line within each test where the user has to be authorized to perform the action.

sign_in User.first

Of course it's dead simple once you know how to do it.