虽然代码触发了事件,但Spy on不会返回true

时间:2016-05-24 14:51:22

标签: javascript typescript angular jasmine karma-runner

我有这个Jasmine 2测试我试图理解为什么它没有通过。

describe('Some functionality', () => {

let fixture;
let nativeElement;
let componentInstance;

beforeEachProviders(() => [
    CenterOnMeButton,
    EventAggregatorService,
    GeoLocationService,
    provide(GoogleMapsAPIWrapper, {useClass: StabGoogleMapsApiWrapper}),
    provide(MapController, {useClass: StabMapControllerService}),
    provide(MapEventsService, {useClass: StabMapEventsService})
]);

beforeEach(injectAsync([TestComponentBuilder, GoogleMapsAPIWrapper, GeoLocationService, MapController
    ], (tcb: TestComponentBuilder ) => {

    return tcb
        .createAsync(CenterOnMeButton).then((componentFixture: ComponentFixture<CenterOnMeButton>) => {

            ({nativeElement, componentInstance} = componentFixture);
            fixture = componentFixture;
            componentFixture.detectChanges();
        });
}));

it('should emit an event when center button is clicked',
    injectAsync([EventAggregatorService], (eventAggregator: EventAggregatorService) => {
    spyOn(eventAggregator, 'trigger');
    spyOn(componentInstance, 'toogleEnableGeolocation');
    nativeElement.querySelector('.toggle.fa.fa-crosshairs').click();
    expect(eventAggregator.trigger).toHaveBeenCalled();
    expect(componentInstance.toogleEnableGeolocation).toHaveBeenCalled();

}));

});

以我为中心按钮    ×单击中心按钮时应发出enableGeolocation事件,但失败:  已经调用了预期的间谍触发器。

我正在监视的方法:

trigger(event: BaseEvent): EventAggregatorService {
    const eventType = event.constructor.name;
    const subject: Subject<BaseEvent> = this._subjects[eventType];
    if (subject) {
        subject.next(event);
    }
    return this;
}

我希望(eventAggregator.trigger).toHaveBeenCalled()为true。这是我调试应用程序时,但测试失败。任何提示?

0 个答案:

没有答案