Avdhut
Avdhut

Reputation: 160

Not getting SPEC HAS NO EXPECTATIONS message for async test case

I am creating test case to demonstrate angular testing with asynchronous code and I want to get SPEC HAS NO EXPECTATIONS, I have following code :

   describe('EmployeeService', () => {
  let service: EmployeeService; 

  beforeEach(() => TestBed.configureTestingModule({
    imports: [HttpClientTestingModule],
    providers: [EmployeeService]
  }).compileComponents());

  beforeEach(() => {
    service = TestBed.get(EmployeeService);
  });

  it('should be return list of employee names ', () => {

        service.getListOfEmpNames().subscribe((data: string[]) => {

        expect(data.length).toBeGreaterThan(0);
    });

  });

});

and the method in EmployeeService:

getListOfEmpNames(): Observable<string[]>{
    return of(['Rohan', 'Ethan', 'Pruthvi']);
  }

As per my understand when we are testing asynchronous code without done or async or fakeasync it will pass the test, but will also display message before description - SPEC HAS NO EXPECTATIONS(please correct my understanding if wrong). Also want to know if there is any configuration that can help to add or suppress such message.

Upvotes: 0

Views: 682

Answers (1)

AliF50
AliF50

Reputation: 18849

I am suspicious like Caramiriel where the subscription is not being fired.

Try:

it('should be return list of employee names ', async(done) => {
  const data = await service.getListofEmpNames().pipe(take(1)).toPromise();
  expect(data.length).toBeGreatherThan(0);
  done();
});

I usually don't like subscribes in my tests because I don't know if it ever got executed. I wait for a promise to complete (then I know it got executed) and in this case if getListOfEmpNames never returns anything, the test will be stuck on resolving the promise thereby giving you better "engineering".

Upvotes: 1

Related Questions