1

I keep getting the error:

UserService unit tests getNumberOfUsers() SPEC HAS NO EXPECTATIONS should return total number of users

When I have defined an expectation as below:

  describe('getNumberOfUsers()', () => {
    it('should return total number of users',
      inject([UserService], (userService) => {
        let noOfUsers = 0;
        userService.getNumberOfUsers().subscribe((result) => {
          noOfUsers = result.data.counter;
          expect(noOfUsers).toBeGreaterThan(0);
        });
    }));

How can I fix it?

methuselah
  • 12,766
  • 47
  • 165
  • 315
  • The subscription is asynchronous (that's the whole point of it...) so there's no expectation reached until *after* the test finishes. [Read the docs.](https://angular.io/guide/testing#service-tests) – jonrsharpe Mar 10 '19 at 21:01

1 Answers1

4

you need to supply jasmine's DoneFn

describe('getNumberOfUsers()', () => {
    it('should return total number of users', (done: DoneFn) => { // supply DoneFn
      inject([UserService], (userService) => {
        let noOfUsers = 0;
        userService.getNumberOfUsers().subscribe((result) => {
          noOfUsers = result.data.counter;
          expect(noOfUsers).toBeGreaterThan(0);
          done(); //call DoneFn
        });
    })(); // immediatly call inject function
  });
})

see for more information

Arikael
  • 1,989
  • 1
  • 23
  • 60
  • 5
    But whenever I use "done", it starts giving me this error: Error: Timeout - Async function did not complete within 5000ms" – Anubha Gupta Jan 14 '20 at 10:27