mercredi 21 juin 2017

Testing an angular service which have return function inside a return function with main code resides on inner return function

My Module and factory is

var testappservice = angular.module('testappservicemodule',[])
testappservice.factory('testappService',function(){
    return function(){
        var add = function(){
            return 'added';
        }
        return function(){
            add : add
       }
    }
})

My Test Suite is

describe('testappService factory Testcases', function() {
    var testappService;

    beforeEach(function () {
        angular.mock.module('testappservicemodule');
    });

    beforeEach(inject(function(_testappService_) {
        testappService = _testappService_;
    }));

    it('testappService should be defined', function() {
        expect(testappService).toBeDefined();
    });

    describe('Test cases related to .add()', function() {
        it('testappService.add should be defined', function() {
            expect(testappService.add).toBeDefined();
        });
    });
});

When I run Karma, It is showing exception as

Test cases related to .add()

✗ testappService.add should be defined

Expected undefined to be defined.

I have searched for a solution for this and couldn't find any. I want to know how can we test this kind of services.

Aucun commentaire:

Enregistrer un commentaire