Search code examples
ruby-on-railsunit-testingruby-on-rails-3functional-testing

How to import Rails helpers in to the functional tests


Hi I recently inherited a project in which the former dev was not familiar with rails, and decided to put a lot of important logic into the view helpers.

class ApplicationController < ActionController::Base
  protect_from_forgery
  include SessionsHelper
  include BannersHelper
  include UsersHelper
  include EventsHelper
end

Specifically session management. This is okay and working with the app but I am having problems writing tests for this.

A specific example. Some of the actions do a before_filter to see if a current_user is an admin. This current_user is normally set by a sessions_helper method that is shared in all our controllers So in order to properly test our controllers I need to be able to use this current_user method

I have tried this:

require 'test_helper'
require File.expand_path('../../../app/helpers/sessions_helper.rb', __FILE__)

class AppsControllerTest < ActionController::TestCase
  setup do
    @app = apps(:one)
    current_user = users(:one)
  end

  test "should create app" do
    assert_difference('App.count') do
      post :create, :app => @app.attributes
  end
end

The require statement finds the session_helper.rb okay but without the Rails magic it is not accessible in the same way with in the AppsControllerTest

How can I spoof this crazy setup to test?


Solution

  • The only solution i found was to re-factor and use a decent auth plugin