我有一个单元测试如下
it('billing information is correct', () => {
fixture.detectChanges();
spyOn(component.myEventEmitter, 'emit').and.callThrough();
component.form.controls['size'].setValue(12);
fixture.detectChanges();
**let args= component.myEventEmitter.emit.mostRecentCall **
expect(args.billingSize).toEqual('30')
});
当大小发生变化时,myEventEmitter 将与一个包含 billingSize 的大型 json 对象一起发出。我希望测试检查这个值是否符合预期。但看起来我无法对事件发射器执行“mostRecentCall/calls”。有什么建议吗??
注意:我不想做
expect(component.myEventEmitter.emit).toHaveBeenCalledWith(*dataExpected*);
因为dataExpected是一个很大的json对象。我只关心一个领域。任何帮助将不胜感激。
最佳答案
这应该有效。
it('billing information is correct', () => {
fixture.detectChanges();
spyOn(component.myEventEmitter, 'emit').and.callThrough();
component.form.controls['size'].setValue(12);
fixture.detectChanges();
let arg: any = (component.myEventEmitter.emit as any).calls.mostRecent().args[0];
expect(arg.billingSize).toEqual('30');
});
注意:
component.myEventEmitter.emit.calls.mostRecent()
- 不会编译(错误:类型上不存在调用 ..')所以将其键入“any”并且应该可以工作。
关于angular - 如何在 Jasmine 测试中获取事件发射器的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46940985/