Promisses test in AngulaJS application using Karma and Jasmine

Hello,

I have an application in AngularJS and I wanted to ensure the quality of my code by doing unit tests and I am using Karma and Jasmine for that. After having had a certain difficulty to configure the dependency injections and etc I was finally able to "access" the functions of my controller and perform the tests. but I am having difficulty doing the tests of the promises , I am not able to find how do I simulate the return of the asynchronous call to be able to proceed with the test of my function.

Would anyone know how to give me help?

This is my Service:

angular.module('projetoServices', [])
.factory('projetoService', ['$http', function ($http) {

    return {
        consultaLink: function (entrada) {
            return $http.post('http://localhost:7001', entrada})
                .then(tratarResposta, tratarErro);
        },
    };

    function tratarResposta(response) {
        if (response.data.data == null) {
            return response.data;
        } else {
            return response.data.data;
        }
    }

    function tratarErro(error) {
        if (error.data.data == null) {
            return error.data;
        } else {
            return error.data.data;
        }
    }

}]);

Follows function inside my controller that I want to test:

vm = this;
vm.abreLink = abreLink;

function abreLink(id) {
        projetoService.consultaLink({ "id": id}).then(function (response) {
            if (response) {
                   var html = response.url;
        $window.open(html, '_Blank',);
            }
        });
}

Inside my test class I am injecting my service through the modulecommonsinit file

(function (angular) {

angular.mock.moduleCommonsInit = function () {

    module(function ($provide) {
        $provide.factory('projetoService', function ($q) {
            return {
                    consultaLink: function () {
                    var defer = $q.defer();
                    defer.resolve({ id: 1, url: www.alura.com.br });
                    return defer.promise;
                },
            };
        });

    });
};
}(angular));

And this is my test:

describe('testes da projeto
Controller', function () {
var $rootScope, $scope, $q, $httpBackend, $controller, vm;

beforeEach(function () {
    angular.mock.moduleCommonsInit();
    angular.mock.module('projeto
Controllers');
});

beforeEach(inject(function (_$rootScope_, _$controller_, _$q_,_$httpBackend_, projetoService) {

    $q = _$q_;
    $rootScope = _$rootScope_;
    $scope = _$rootScope_.$new();
    $httpBackend = _$httpBackend_;
    $controller = _$controller_;

    vm = _$controller_('projetoController', {
        $scope: $scope,
        projetoService : projetoService
    });

}));

it('Teste AbreLink()...', function () {
    var id = "1";

    vm.abreLink(id);

});

});
Author: Rafael Silva, 2019-07-01

1 answers

Things I see missing:

  • tests must know when they are complete

When you have it('Teste AbreLink()...', function () {, and since you are doing asynchronous testing, you have to return a Promise or use the argument of that function. I suggest you use it('Teste AbreLink()...', function (done) { and then call this function done that is passed to you in the arguments to finish the test.

  • the function tested does not indicate that it has been completed

When you use abreLink(id) this function does not give test... that is, it is not possible to know when it actually opens another window. I suggest adding an argument to this function that runs when the window loads. For example: abreLink(id, cb) and the function be defined like this:

function abreLink(id, cb) {
    projetoService.consultaLink({ "id": id}).then(function (response) {
        if (response) {
           var html = response.url;
           var win = $window.open(html, '_Blank');
           win.addEventListener('load', cb);
        }
    });
}

So you can run the test like this:

it('Teste AbreLink()...', function (done) {
    var id = "1";
    vm.abreLink(id, done);
});
 0
Author: Sergio, 2019-07-01 21:13:44