0

My backend retrieves me a 'x-token' when a user log in successfully on my web. On my frontend I do a simple validation, getting this token and setting: $rootScope.authenticated = true; if the token is not present on the header response for any reason my frontend sets: $rootScope.authenticated = false;

My Controller

signinService
    .signin(datafromForm)
    .then(function (data) {
        if ( $myTokenManager.save(data.headers('x-token')) ) {
            $rootScope.authenticated = true;
        } else {
            $rootScope.authenticated = false;
            // Unit test is sent it here :(
        }

    });

This is working perfect, now I need to do a unit test for this method, but my test is not generating or setting the header response properly. What am I doing wrong on my test?

describe('Testing controllers', function() {

    describe('MyController unit test', function() {

        var $httpBackend, $rootScope, createController, authRequestHandler, datafromForm;

        beforeEach(module('myapp'));

        beforeEach(inject(function($injector) {

            $httpBackend = $injector.get('$httpBackend');

            authRequestHandler = $httpBackend
                                    .when(
                                        'POST',
                                        'http://mywebapp:8080/loginuser',
                                        {"username":"admin", "password":"admin"}
                                    ).respond({'x-token': 'XXX'});

            $rootScope = $injector.get('$rootScope');
            var $controller = $injector.get('$controller');

            createController = function() {
                return $controller('MyController', {'$scope' : $rootScope});
            }
        }));

        //
        afterEach(function() {
            $httpBackend.verifyNoOutstandingExpectation();
            $httpBackend.verifyNoOutstandingRequest();
        });

        //
        it('should authentication user', function() {

            var controller = createController();

            //
            $rootScope.username = "admin";
            $rootScope.password = "admin";

            datafromForm = {
                username: $rootScope.username,
                password: $rootScope.password
            };

            $httpBackend.expectPOST(
                'http://mywebapp:8080/loginuser',
                {"username":"admin", "password":"admin"}
            ).respond(
                200, 
                {'x-token': 'XXX'}
            );

            $rootScope.login(formData);
            $httpBackend.flush();
            expect($rootScope.authenticated).toBe(true); // This is getting false :(
        });





    });

});

I'm following this example on angular documentation.

1 Answer 1

1

The documented signature of the respond() function is

function([status,] data[, headers, statusText])

In your first respond(), you only pass data, and no header:

respond({'x-token': 'XXX'});    

In your second respond(), you only pass status and data, and still no header:

respond(
    200, 
    {'x-token': 'XXX'}
);
Sign up to request clarification or add additional context in comments.

1 Comment

@razorblade desperately needing your quizzes :-) Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.