mercredi 25 janvier 2017

How to test a modul with two dependencies and a factory method in Jasmine

Maybe this question is answered in other questions, but i don't understand how it works with dependencies :/

I have watch several tutorials and can't find the solution :/

I have a module which has 2 dependencies and factory method.

How can I test the factory method ?

This is what I got so far :

test :

describe("Test all services from the home module", function() {

    describe("when I call factory method: GetResultFile", function() {
        //declare variable for dependencies on module/factory method
        var GetResultFile;
        var mockNgNaifBase64;
        var mockNgFileSaver;

        beforeEach(module('Home'));

        beforeEach(module('naif.base64'));

        beforeEach(inject(function(_naif.base64_) {
            mockNgNaifBase64 = _naif.base64_;
        }));

        beforeEach(module('ngFileSaver'));

        beforeEach(inject(function(_ngFileSaver_) {
            mockNgFileSaver = _ngFileSaver_;
        }));

        beforeEach(inject(function() {
            var $injector = angular.injector(['Home']);
            GetResultFile = $injector.get('GetResultFile');
        }));

        it("when I call factory method: GetResultFile.arrayCheck", function() {
            var statusInArray = 'SETUP';
            var statusNotInArray = 'DONE';
            var statusList = [{'id': 1, 'status': 'SETUP'}, {'id': 2, 'status': 'TEST'}];

            expect(GetResultFile.arrayCheck(statusInArray, statusList)).not.toEqual(-1);
            expect(GetResultFile.arrayCheck(statusNotInArray, statusList)).toEqual(-1);
        })  
    })

});

my module is this : angular.module('Home', ['naif.base64', 'ngFileSaver']);

and my factory :

.factory('GetResultFile',
    ['$http', '$q',
    function ($http, $q) {

        var service = {};

        //service function for checking, if item is already in the array
        service.arrayCheck = function(status, items) {

            var i = 0;
            var len = items.length;

            for (i = 0; i < len; i++) {
                //if item is already in array: delete item
                if(status === items[i].status) {
                    return i;
                }
            }
            return -1;
        }
        return service;
}])

Aucun commentaire:

Enregistrer un commentaire