Wednesday, 15 August 2012

javascript - Angular Jasmine unit test for promise -



javascript - Angular Jasmine unit test for promise -

i want write unit test displays behavior of creating 2 promises, wrapping them 1 $q.all , test promises both resolved @ same time.

describe("application controller", function() { var scope; var controller; beforeeach(module('my.namespace')); //this suite can removed. describe("application ", function() { beforeeach( inject(function ($rootscope, $controller,$q) { scope = $rootscope.$new(); controller = $controller('application', { '$scope': scope }); })); it("should bundle 2 promises one", function (done) { inject(function ($rootscope) { settimeout(function () { $rootscope.$apply(function () { var promiseone = $q.defer(), //promiseonedata; promisetwo = $q.defer(); //promisetwodata promiseone.then(function(data){ promiseone.resolve('promiseone'); expect(1).tobe(1); }); promisetwo.then(function(data){ promisetwodata.resolve('promisetwo'); }) var allpromises = $q.all([promiseone,promisetwo]); allpromises.then(function(data){ //data should contain array of 2 empty elements each promise expect(data.length).tobe(2); }); done(); }); }, 1000); }) });

with error: error: timeout - async callback not invoked within timeout specified jasmine.default_timeout_interval. don't want utilize request here, need 2 promises resolved , moved 1 promise contains both. how can angular , jasmine?

what want spy , see if has been called or not.

describe('test $q', function() { var $scope; $controller; $httpbackend; beforeeach(function() { module('mymodule'); inject(function(_$rootscope_, _$controller_) { $scope = _$rootscope_.$new(); $controller = _$controller_; $controller ('myctrl', { $scope: $scope }); }); it('should test $q.all calls callback when dependent promises resolved', function() { var deferone = $q.defer(), defertwo = $q.defer(); combinedspy = jasmine.createspy('combined'); $q.all([deferone.promise, defertwo.promise]).then(combinedspy); expect(combinedspy).tonothavebeencalled(); deferone.resolve(); defertwo.resolve(); $scope.apply(); expect(combinedspy).tohavebeencalled(); });

this test title pretty confusing, not simulating promise. testing promise. , don't have to, there tests $q in angular itself, there no point in writing tests that?

wrapping them 1 $q.all

that creates 3rd promise. 3rd promise resolved when both promise , b have been resolved.

test promises both resolved @ same time

javascript single threaded, cannot resolved @ same time. 3rd promise, 1 created $q.all(), resolved when first , sec have both been resolved. time may pass between , b beingness resolved.

say resolved, hr later b resolved. c ($q.all) resolved in next digest cycle (by $scope.apply()).

javascript angularjs unit-testing jasmine

No comments:

Post a Comment