Topic: Error with functional test after adding before_filter :login_required

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:

class UnitsController < ApplicationController
  [b]before_filter :login_required[/b]

  def index
    @units = Unit.all
  end

  def show
    @unit = Unit.find(params[:id])
  end

  def new                                                                                                                                                                    
    @unit = Unit.new
  end
 ...

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:

Loaded suite test/functional/units_controller_test
Started
FEFFFEFFF

  1) Failure:
test_create_invalid(UnitsControllerTest) [test/functional/units_controller_test.rb:34]:
expecting <"new"> but rendering with <"">

  2) Error:
test_create_valid(UnitsControllerTest):
ActionController::RoutingError: No route matches {:action=>"show", :controller=>"units"}
    test/functional/units_controller_test.rb:40:in `test_create_valid'

  3) Failure:
test_destroy(UnitsControllerTest) [test/functional/units_controller_test.rb:63]:
Expected response to be a redirect to <http://test.host/units> but was a redirect to <http://test.host/login>.

  4) Failure:
test_edit(UnitsControllerTest) [test/functional/units_controller_test.rb:45]:
expecting <"edit"> but rendering with <"">

  5) Failure:
test_index(UnitsControllerTest) [test/functional/units_controller_test.rb:17]:
expecting <"index"> but rendering with <"">

  6) Error:
test_new(UnitsControllerTest):
NoMethodError: undefined method `login_as' for test_new(UnitsControllerTest):UnitsControllerTest
    test/functional/units_controller_test.rb:26:in `test_new'

  7) Failure:
test_show(UnitsControllerTest) [test/functional/units_controller_test.rb:22]:
expecting <"show"> but rendering with <"">

  8) Failure:
test_update_invalid(UnitsControllerTest) [test/functional/units_controller_test.rb:51]:
expecting <"edit"> but rendering with <"">

  9) Failure:
test_update_valid(UnitsControllerTest) [test/functional/units_controller_test.rb:57]:
Expected response to be a redirect to <http://test.host/units/298486374> but was a redirect to <http://test.host/login>.

Finished in 1.50476625 seconds.

9 tests, 9 assertions, 7 failures, 2 errors, 0 pendings, 0 omissions, 0 notifications
0% passed

5.98 tests/s, 5.98 assertions/s

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

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

Re: Error with functional test after adding before_filter :login_required

Additionally, I have a  controller_authentication.rb file in my /lib folder. The content of the file:

module ControllerAuthentication
  def self.included(controller)
    controller.send :helper_method, :current_user, :logged_in?, :redirect_to_target_or_default
  end

  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end

  def logged_in?
    current_user
  end

  def login_required
    unless logged_in?
      store_target_location
      redirect_to login_url, :alert => "You must first log in or sign up before accessing this page."
    end
  end

  def redirect_to_target_or_default(default, *args)
    redirect_to(session[:return_to] || default, *args)
    session[:return_to] = nil
  end

  private

  def store_target_location
    session[:return_to] = request.url
  end
end

It seems, that the tests are not able to login. Does this file help in any way?

Re: Error with functional test after adding before_filter :login_required

I found a way to solve my problem by using fixtures. The tests run without any errors and I wrote a short article about my solution at:

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