lundi 7 août 2017

Unexpected request: POST

I'm testing some legacy code and i'm facing an unexpected request to the backend.

First of all, let me introduce the code.

The code below corresponds to the test:

'use strict';
describe('login-controller :: TEST', function () {
    var q, scope, rootScope, httpBackend, controller, deferred, 
        authService;

    beforeEach(module('app'));

    beforeEach(inject(function(_$rootScope_, _$httpBackend_, 
        _$controller_, _$q_, _AuthenticationService_) {
        q = _$q_;
        deferred = q.defer();
        rootScope = _$rootScope_;
        scope = _$rootScope_.$new();
        httpBackend = _$httpBackend_;
        authService = _AuthenticationService_;

        controller = _$controller_('login-controller', {$scope: 
        scope});
    }));

    describe('Init setup :: ', function () {
        it('Controller is defined', function () {
            expect(controller).toBeDefined();
        });
    });

    describe('login method :: ', function() {
        beforeEach(function() {
            spyOn(authService, 'Login').and.returnValue(deferred.promise);
        });

        it('Login succeed with custom login params', function() {
            deferred.resolve('ok');
            controller.logInPlatform('fake@mail.com', 'aaa222BBB');

            scope.$apply();

            expect(authService.Login).toHaveBeenCalledWith('fake@mail.com', 'aaa222BBB');
            expect(scope.loginSucceed).toBeTruthy();
            expect(scope.endTest).toBeTruthy();
        });
    });
});

This one, the tested code:

'use strict';
var app = angular.module('mogApp');
app.controller('test-monitoring-controller', ['$scope', '$rootScope', 
'AuthenticationService',
function ($scope, $rootScope, AuthenticationService) {
    var controller = this;

    $scope.endTest = false;
    $scope.loginSucceed = false;

    controller.logInPlatform = function(username, password) {
        var loginUser = username || 'other.fake@mail.com',
            loginPswd = password || 'password';

        AuthenticationService.Login(loginUser, loginPswd)
            .then(
                function(response) {
                    console.log(response);
                    $scope.loginSucceed = true;
                },
                function(error) {
                    console.error(error);
                    $scope.loginSucceed = false;
                }
            )
            .finally(function () {
                $scope.endTest = true;
                $rootScope.loadingView = false;
            });
    };

    controller.logInPlatform();

}]);

And now, here's my problem:

When I run the test, in seems that the spyOn is not running properly, and the code inside the AuthenticationService.Login method is being executed.

And, of course, throws me the following error:

Error: Unexpected request: POST http://ift.tt/2wmA72E

To be honest, I can't understand why Jasmine's spyOn is not working. Any idea or help will be appreciated.

For more information, my tech stack is AngularJS as frontend framework and Symfony as backend. I'm using Jasmine test suite and karma test runner for unittest.

Thank you all.

Aucun commentaire:

Enregistrer un commentaire