【问题标题】:unit test if publish was executed inside another publish单元测试是否在另一个发布中执行发布
【发布时间】:2021-08-13 17:13:09
【问题描述】:

是否可以测试 eventAggregator.publish 是否在另一个发布期间执行?

this.eventAggregator.with(this).subscribe(Xevent, (event: Xevent) => {
    this.eventAggregator.publish(new anotherEvent());
});

如果我这样测试:

describe('when X event is published', () => {
            it('then Y event should be published', () => {
                //arrange
                let event = new Xevent();
                spyOn(evtAggregator, 'publish');
                //act
                evtAggregator.publish(event);
                //assert
                expect(evtAggregator.publish).toHaveBeenCalledWith(new anotherEvent());
            });
        });

jasmine 给我一个错误,事件聚合器是用“Xevent”调用的:

预期的间谍发布已被 [ anotherEvent ... ] 调用 但实际调用是 [Xevent ...]

我可以用不同的方式断言它还是我在排列部分中缺少了什么?

【问题讨论】:

    标签: angularjs unit-testing jasmine


    【解决方案1】:

    我必须为间谍添加“.and.callThrough()”:

    describe('when X event is published', () => {
                it('then Y event should be published', () => {
                    //arrange
                    let event = new Xevent();
                    spyOn(evtAggregator, 'publish').and.callThrough(); //here is the change
                    //act
                    evtAggregator.publish(event);
                    //assert
                    expect(evtAggregator.publish).toHaveBeenCalledWith(new anotherEvent());
                });
            });
    
    

    比我想象的要简单。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      • 2018-03-27
      • 2017-01-16
      • 2016-05-16
      • 2019-01-14
      相关资源
      最近更新 更多