Main Contents

Functional Rails Tests That Require Login

ruby, test driven development, software development

Since as soon as you add login to a site your functional tests are going to require a login, there is of course an easy fix documented here. You just add a login method to your test_helper.rb file. Then you call it in your setup method:

def setup
  @controller  = LoginController.new
  @request     = ActionController::TestRequest.new
  @response    = ActionController::TestResponse.new
  login
  @controller  = AdminController.new
end

I made a silly mistake at first and called login right after:

@controller = LoginController.new

Of course I was greeted with a whole bunch of:

RuntimeError: @request is nil: make sure you set it in your test's setup method.

Simple fix was to move it after the creation of the request and response objects needed by the login method. At least the error message made it a quick duh! moment.

Ed Gibbs @ April 25, 2006

Leave a comment


Feed