1

I'm facing some weird issue with a test suite I set up.

Template :

<a href="#" (click)="blop($event)"></a>

HomeComponent :

export class HomeComponent {
  public test: boolean;

  public constructor () {
    this.test = false;
  }

  public blop($event) {
    this.test = true;
  }
}

The test :

describe('Home Component : ', () => {
  var builder;
  var app;

  beforeEach(inject([HomeComponent, TestComponentBuilder], (app, tcb) => {
    builder = tcb;
    app = app;
  }));

  it('should return test = true after click...', async(() => {

    builder.createAsync(HomeComponent).then((fixture: ComponentFixture<HomeComponent>) => {

      spyOn(fixture.componentInstance, 'blop');

      fixture.detectChanges();

      var compiled = fixture.debugElement.nativeElement;
      compiled.querySelector('a').click();
      expect(fixture.componentInstance.test).toBe(true);
      expect(fixture.componentInstance.blop).toHaveBeenCalled();
    });
  }));
});

I get an error output on : Expected false to be true.

BUT, if I comment the spy and the last expect, it works.

Do you have any idea about what I am doing wrong ?

Thanks

M

4

1 回答 1

1

您应该为间谍返回一个值。SpyOn 仅开始监视。

SpyOn(fixture.componentInstance, 'blop').and.returnValue(true);

于 2017-01-04T09:50:05.170 回答