When running the following Angular (4) test for a service, it appears to pass before the Observable returns and hits the expect
statement.
it('should enter the assertion', inject(
[ MockBackend, CellService ],
( backend: MockBackend, s: CellService ) => {
const urls = [];
backend.connections.subscribe((connection: MockConnection) => {
const req = connection.request;
urls.push(req.url);
if (req.method === RequestMethod.Get && req.url === '/api/getTest') {
connection.mockRespond(new Response(new ResponseOptions('enter mocked content')));
}
});
s.getCell('powders').subscribe(val => expect(true).toBeFalsy())
})
);
I attempted to use async/await, but it didn't have any effect. What approach should I take here?
Update:
This section of code also passes ...
it('should enter the assertion', async(inject(
[ MockBackend, CellService ],
( backend: MockBackend, s: CellService ) => {
const urls = [];
backend.connections.subscribe((connection: MockConnection) => {
const req = connection.request;
urls.push(req.url);
if (req.method === RequestMethod.Get && req.url === '/api/getTest') {
connection.mockRespond(new Response(new ResponseOptions('enter mocked content')));
}
});
s.getCell('powders').subscribe(val => expect(true).toBeFalsy())
})
));