Unit Testing A Angularjs Service?
Hey, I need help with my testing service. I have this service: MyService.js And this controller: angular.module('MyControllers', []) .controller('MyCtrl2', ['$scope', 'FnEncode',
Solution 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 functionsit('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
});
});
});
Post a Comment for "Unit Testing A Angularjs Service?"