javascript - AngularJS: $digest required before $httpBackend.flush() -


i trying wrap head around why need call $rootscope.$digest() before $httpbackend.flush() test pass.

if don't getting error: no pending request flush!

here test:

it('should post new task server', function() {      $httpbackend.when('post', $rootscope.serverroot + '/task/create').respond({});      var created = false;      mockboardservice.addtask({name: 'add dynamic categories'})         .then(function(response) {             created = true;         }     );      $rootscope.$digest();      $httpbackend.flush();      expect(created).tobe(true); }) 

and service function it's calling:

this.addtask = function(data) {     return $http.post($rootscope.serverroot + '/task/create', data);             } 

why need run $rootscope.$digest?

it looks mockboardservice.addtask doing async work. without $digest, it's trying flush() $httpbackend requests before async code has chance make request. $digetst() call giving time async work. correct way (with jasmine 2.0, it's little different in 1.3) like:

it('should post new task server', function(done) {      $httpbackend.when('post', $rootscope.serverroot + '/task/create').respond({});      var created = false;      mockboardservice.addtask({name: 'add dynamic categories'})         .then(function(response) {             created = true;             done();         }     );      $httpbackend.flush();      expect(created).tobe(true); }) 

note 'done' passed in function in 'it'

see http://jasmine.github.io/2.0/introduction.html#section-asynchronous_support


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -