单元测试发布是否在另一个发布中执行

unit test if publish was executed inside another publish

是否可以测试 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”调用的:

Expected spy publish to have been called with [ anotherEvent ... ] but actual calls were [ Xevent ...]

我可以用不同的方式声明它还是我在安排部分缺少什么?

我不得不为间谍添加“.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());
            });
        });

比我想象的要简单。