Question

Hey, I need help with my testing service.

I have this service: MyService.js

And this controller:

angular.module('MyControllers', [])

.controller('MyCtrl2', ['$scope', 'FnEncode', function ($scope, FnEncode) {

        $scope.encoded1 = "";
        $scope.encoded2 = "";
        $scope.encoded3 = "";

        $scope.encode1 = function() {
            $scope.encoded1 = FnEncode.encodeUUI(FnEncode.encodeFunctionalNumber($scope.numberToEncode));
        };

        $scope.encode2 = function() {
            $scope.encoded2 = FnEncode.encodeUUI(FnEncode.encodeFunctionalNumber($scope.numberToEncode)+
                FnEncode.encode2($scope.EncodeWith2));
        };

        $scope.encode3 = function() {
            $scope.encoded3 = FnEncode.encodeUUI(FnEncode.encodeFunctionalNumber($scope.numberToEncode)+
                FnEncode.encode3($scope.EncodeWith3));
        };
    }]);

The service is basically doing when I enter 123 in a text field it outputs me 00050221F3, where the first 00 are encodeUUI. I did test something like this but it says Cannot read property encoded1:

'use strict';

describe('My services', function() {

    beforeEach(module('myApp'));

    beforeEach(module('MyServices'));

    describe('MyCtrl2', function() {

       var scope, ctrl;
        beforeEach(inject(function($rootScope, $controller) {
            scope = $rootScope.$new();
            ctrl = $controller('MyCtrl2', {$scope: scope});
        }));

        it('should output: ', function(scope) {
            expect(scope.encoded1.toBe("00050221F3"));     
        });
    });
});

I hope somebody can tell me where I am doing wrong?

Was it helpful?

Solution

You're not calling your function to encode the value, try:

it('should output: ', function() {
     scope.numberToEncode = "123";
     scope.encode1();
     expect(scope.encoded1.toBe("00050221F3"));     
});

However, this is not how we should unit test the service. To unit test the service, we test each function of the service separately.

This kind of test to verify the function of the scope.encode1 is also not correct. We should mock the FnEncode and verify that FnEncode's functions were called with expected order.

To test your service, you should do something like this:

'use strict';

describe('My services', function() {

    beforeEach(module('myApp'));

    beforeEach(module('MyServices'));

    describe('MyCtrl2', function() {

        var encode;
        beforeEach(inject(function(FnEncode) {
            encode = FnEncode; //inject your service and store it in a variable
        }));

        //Write test for each of the service's functions

        it('encode Functional Number: ', function() {
            var encodedValue = encode.encodeFunctionalNumber("123");
            expect(encodedValue).toBe("00050221F3");  //replace 00050221F3 with your expected value
        });

        it('encode UUI: ', function() {
            var encodedValue = encode.encodeUUI("123");
            expect(encodedValue).toBe("00050221F3");  //replace 00050221F3 with your expected value
        });
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top