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 calltasksservice.removeandgetnext
- when
tasksservice.removeandgetnext
done should change location (invokegoto
)
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
Post a Comment