javascript - Unit Testing a AngularJS service? -


hey, need testing service.

i have service: myservice.js

and 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 doing when enter 123 in text field outputs me 00050221f3, first 00 encodeuui. did test 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 can tell me doing wrong?

you're not calling function encode value, try:

it('should output: ', function() {      scope.numbertoencode = "123";      scope.encode1();      expect(scope.encoded1.tobe("00050221f3"));      }); 

however, not how should unit test service. unit test service, test each function of service separately.

this kind of test verify function of scope.encode1 not correct. should mock fnencode , verify fnencode's functions called expected order.

to test service, should 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 service , store in variable         }));          //write test each of service's functions          it('encode functional number: ', function() {             var encodedvalue = encode.encodefunctionalnumber("123");             expect(encodedvalue).tobe("00050221f3");  //replace 00050221f3 expected value         });          it('encode uui: ', function() {             var encodedvalue = encode.encodeuui("123");             expect(encodedvalue).tobe("00050221f3");  //replace 00050221f3 expected value         });     }); }); 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -