Faustas - Programming, Projects, Psychology and Faust
I added the before_filter :login_required authentication within my controllers that require a login for the routes of the specific controllers. After executing the test, I got several errors regarding the routes. The tests have to login during the run.
Let's solve the problem!
Let me introduce one of my controllers:
class UnitsController < ApplicationController before_filter :login_required def index @units = Unit.all end .....
I added the before_filter :login_required line to require a login for the routes of the controller.
In the next step, you have to add a user into your users.yml file stored in /test/fixtures.
An example would be:
#password: password test: username: test email: test@example.com password_hash:
$2a$10$93UPbJulutAwtfrkEYs0lOH3G6E5ryGBZ9.1EuKFjC9tlEE2JIxXu password_salt: $2a$10$93UPbJulutAwtfrkEYs0lO
You have to tell the test, that he must use the defined fixtures to login during the test.
Add the following lines to your units_controller_test.rb:
require 'test_helper' class UnitsControllerTest < ActionController::TestCase self.use_instantiated_fixtures = true fixtures :users def setup user = User.authenticate('test', 'password') if user session[:user_id] = user.id end end def teardown session[:user_id] = nil end def test_index get :index assert_template 'index' end ....
You have to add the fixtures, setup and teardown parts to all tests with controllers containing a before_filter :login_required requirement.
Comments
Danke! :-)
By Anonymous on 01 Apr 2011 at about 15:17.Ein echter Lebensretter. ;-)