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
before_filter :login_required
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.
Update:
I have a controller_authentication.rb file in my /lib directory. The content of the file is as follows:
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
Do you have any idea, how I have to change the tests to execute them without errors? The tests have to authenticate for the controllers.
Since you are requiring authentication to access the controller actions, your tests also need to be authenticated.
It looks like you are using RESTfulAuthentication, in your lib directory there should be an authenticated_test_helper.rb
providing methods for setting the appropriate session variable to authenticate your tests.
精彩评论