Testing async (Promise) methods in Angular 2 Services Testing async (Promise) methods in Angular 2 Services angular angular

Testing async (Promise) methods in Angular 2 Services


The problem was to not call the inject function.

The test code for the service now looks like this:

it("should be able to set a spy on the scanner and test the service", done => {    const testBC = "123456";    const spy = spyOn(TestBed.get(BarcodeScanner), "scan");    spy.and.callFake(() => {        return new Promise((resolve, reject) => {            resolve(testBC);        })    });    inject([TestService], (service) => {        service.testScanner().then(res => {            expect(res).not.toBe(testBC);            done()        }, reason => {            expect(true).toBe(false);            done();         })    })(); //<-- do not forget these braces!!});