gfpacheco
gfpacheco

Reputation: 3215

Error testing Parse Promise and $http request together

I'm trying to test a code that waits for a Promise before calling http:

Code:

function foo() {
  return Parse.Promise.as(1).then(function(one) {
    return $http.get('/bar');
  });
}

Test:

describe('foo', function() {
  it('gets', function(done) {
    $httpBackend.expect('GET', '/bar').respond(200);
    foo().then(function(res) {
      expect(res.status).to.be.equal(200);
      done();
    });
    $httpBackend.flush();
  });
});

Error:

1) gets
     foo
     No pending request to flush !

My guess is that beacuse Parse.Promise delays the promise resolution, and the http request wasn't made when the $httpBackend.flush is called.

Is there any workaround for this?

Upvotes: 1

Views: 32

Answers (2)

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276306

Mocha actually has promise syntax support, you can just test promises directly by dropping the done parameter and returning the promise:

describe('foo', function() {
  it('gets', function() { // no `done`
    try { 
      $httpBackend.expect('GET', '/bar').respond(200);
      return foo().then(function(res) { // return here
        expect(res.status).to.be.equal(200);
      });
    } finally {
      $httpBackend.flush();
    }
  });
});

Upvotes: 1

gfpacheco
gfpacheco

Reputation: 3215

By default Parse Promise delays to the next tick or with timeout 0.

You can disable this behavior calling Parse.Promise.disableAPlusCompliant(); before your test.

Upvotes: 0

Related Questions