如何在茉莉花测试中获取事件发射器的参数

Jos*_*osf 5 typescript karma-jasmine spyon angular2-testing angular

我有一个单元测试如下

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')
});
Run Code Online (Sandbox Code Playgroud)

当大小发生变化时,myEventEmitter 会与一个包含 billingSize 的大型 json 对象一起发出。我希望测试检查这个值是否符合预期。但看起来我无法在事件发射器上执行“mostRecentCall/calls”。有什么建议??

注意:我不想做

 expect(component.myEventEmitter.emit).toHaveBeenCalledWith(*dataExpected*);
Run Code Online (Sandbox Code Playgroud)

因为 dataExpected 是一个很大的 json 对象。我只关心一个领域。任何帮助将非常感激。

Jos*_*osf 6

这应该有效。

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');
});
Run Code Online (Sandbox Code Playgroud)

笔记:

 component.myEventEmitter.emit.calls.mostRecent() 
Run Code Online (Sandbox Code Playgroud)

- 不会编译(错误:类型 ..' 上不存在调用),因此将其键入为“any”并且应该可以工作。