4

I have the spec that has to expect still it says there are no expectations...

it('should click on yes button of technician and check save&continue functionality', () => {
    const saveAndContinue = fixture.debugElement.query(By.css(saveContinueBtn)).nativeElement;
    saveAndContinue.click();
    fixture.detectChanges();
    fixture.whenStable().then(() => {
        const spy = spyOn(component,'isSaveAndContinueClicked').and.callThrough();
        expect(component).toBeDefined();
        expect(spy);
        component.isSaveAndContinueClicked();
        expect(component.isSaveAndContinueClicked).toHaveBeenCalled();
        const yesbutton = fixture.debugElement.query(By.css('#yesButton')).nativeElement;
        expect(yesbutton).toBeTruthy();
        fixture.detectChanges();
        fixture.whenStable().then(() => {
            spyOn(component, 'change').and.callThrough();
            spyOn(component, 'change2').and.callThrough();
            yesbutton.click();
            expect(component.change).toHaveBeenCalled();
            expect(component.change2).toHaveBeenCalled();
        });
   });
});

It throws the error as spec testComponent should click on yes button of the technician and check save&continue functionality has no expectations... Can you please suggest...

Justin
  • 159
  • 1
  • 2
  • 13
user1498069
  • 124
  • 2
  • 14

2 Answers2

6

You should add your callback within an async or fakeAsync block or else all your code would run synchronously without encountering any expects.

This is because you are having assertions inside fixture.whenStable().then(() => {....}) which runs asynchronously.

it('should click on yes button of technician and check save&continue 
  functionality', async(() => {
    const saveAndContinue = 
    fixture.debugElement.query(By.css(saveContinueBtn)).nativeElement;
    saveAndContinue.click();
    ........

}));
Amit Chigadani
  • 28,482
  • 13
  • 80
  • 98
2

In addition to Amit's answer:

Since Angular 10 async can be replaced with waitForAsync. Using async also gives a deprecation warning. It is removed from v12 on.

Angular <= 11: it("description", async(() => {}));
Angular >= v12: it("description", waitForAsync(() => {}));

If you stick to those versions, you should be able to use fakeAsync() and fixture.whenStable().then() as expected.

C4d
  • 3,183
  • 4
  • 29
  • 50