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?
You're not calling your function to encode the value, try:
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 theFnEncode
and verify thatFnEncode
's functions were called with expected order.To test your service, you should do something like this: