javascript - Unit-test promise-based code in Angularjs -


i'm having hard times trying test promise-based code in angularjs.

i have following code in controller:

    $scope.markasdone = function(taskid) {         tasksservice.removeandgetnext(taskid).then(function(nexttask) {             goto(nexttask);         })     };      function goto(nexttask) {         $location.path(...);     } 

i'd unit-test following cases:

  • when markasdone called should call tasksservice.removeandgetnext
  • when tasksservice.removeandgetnext done should change location (invoke goto)

it seems me there no easy way test 2 cases separately.

what did test first 1 was:

var nooppromise= {then: function() {}} spyon(tasksservice, 'removeandgetnext').andreturn(nooppromise); 

now test second case need create fake promise resolved. it's quite tedious , it's lot of boilerplate code.

is there other way test such things? or design smell?

you still need mock services , return promise, should use real promises instead, don't need implement functionality. use beforeeach create fulfilled promise , mock service if need resolved.

var $rootscope;  beforeeach(inject(function(_$rootscope_, $q) {   $rootscope = _$rootscope_;    var deferred = $q.defer();   deferred.resolve('somevalue'); //  resolved, can spec    // jasmine 2.0   spyon(tasksservice, 'removeandgetnext').and.returnvalue(deferred.promise);     // jasmine 1.3   //spyon(tasksservice, 'removeandgetnext').andreturn(deferred.promise);   })); 

if you'd rather prefer resolve in each it block different value, expose deferred local variable , resolve in spec.

of course, keep tests are, here simple spec show how work.

it ('should test receive fulfilled promise', function() {   var result;    tasksservice.removeandgetnext().then(function(returnfrompromise) {     result = returnfrompromise;   });    $rootscope.$apply(); // promises resolved/dispatched on next $digest cycle   expect(result).tobe('somevalue'); }); 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -