Topic: Testing framework & how to test application controller before_filter?

I've created an application which uses the restful_authentication and globalize plugins. Now I'm in a position where I'd like to test the before_filters that have been added to my application controller - they do things like cookie-based login, setting the header content type and setting the locale.

I've been trying to test the application controller, but I don't think the before filters run unless a request has been issued. Trying to use an integration test for checking that the locale is set properly gives me other problems - it seems that integration tests behave differently to functional tests when it comes to user login.

I think the main problem is that I do not have a thorough grasp of what environment the different types of tests operate in - e.g. what objects are available.

1. Can anyone point me in the direction of some good resources about how the Rails test framework operates, with regard to the different environments that the different type of tests operate in?

2. Is there a way to test the application controller without having to do so "by proxy" via testing a specific controller?

Many thanks.

Steph

Re: Testing framework & how to test application controller before_filter?

I'm quite surprised no one's responded to this - surely there's a need to test that the application controller has set up the application's environment properly (i.e. set the content type, locale, login cookies, etc).

I've left this problem for a while but I'm back on it now. Hopefully I'll figure it out, at which point I'll post my findings.

Re: Testing framework & how to test application controller before_filter?

I've managed to test the application controller's before filters by adding a test action to it and calling that action from the functional test. It'd be nice if there was a better way to do this, or if I could hide the action to everything but the test, but I'm not sure how to do this.

1. I added the following to the application controller:

  def test
   render :text => 'test'
  end

2. Then called the action in my application controller test:

require File.dirname(__FILE__) + '/../test_helper'
require 'application'

# Re-raise errors caught by the controller.
class ApplicationController; def rescue_action(e) raise e end; end

class ApplicationControllerTest < Test::Unit::TestCase

  def setup
    @controller = ApplicationController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
  end

  def test_content_type
    get :test
    assert_equal 'text/html; charset=utf-8', @response.headers['Content-Type']
  end

  def test_should_set_default_locale
    get :test
    # Base language code must not be nil in order to set the locale.
    assert_not_nil Locale.base_language.code, "base locale set"
    assert Locale.active?, "locale active"
    assert_equal Locale.base_language.code, Locale.active.language.code, "locale is base locale"
  end

end