0

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?

1
  • Why are you using $scope.encoded1 and $scope.encode1? You could just use $scope.encode1 and have the function return the result of encodeUUI() rather than assigning it to encoded1. Commented Apr 14, 2014 at 11:42

1 Answer 1

1

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
        });
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!!!! Its working. I just changed the last line in: expect(encode.encodeUUI(encodedValue)).toBe("00050221F3");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.