mercredi 30 novembre 2016

Testing Angular 2 service that returns a Promise

I'm trying to unit test a service which should use fake http backend, provided by Angular's in-memory data service. This is the relevant code:

describe('getCars() method ', ()  => {
      it('should return a resolved Promise', inject([DataService], (service: DataService) => {
        service.getCars().then((value) => {
          expect(value.length).toBe(3);
       });
    }));
  });

The problem is I can't use Jasmine's done callback to treat the asynchronous service.getCars() call, because of how inject function works. I can't use async test helper neither, because it can't work with promises. So I have no idea how to wait for promise to return---the test just runs without ever reaching expect.

Aucun commentaire:

Enregistrer un commentaire