angular - 如何在 Jasmine 测试中获取事件发射器的参数

标签 angular typescript karma-jasmine angular2-testing spyon

我有一个单元测试如下

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/

相关文章:

json - 循环对象 Ionic 3/Angular 4

node.js - Angular:ngModel 插值在输入文本框中返回 NaN。如何显示号码

angularjs - 将 Angular 2.21 升级到 2.25 时,Jasmine httpBackend 相关测试中断

javascript - ng2 智能表格复选框不会在所有页面上持久存在

javascript - 从可观察的结果中获得独特的结果

angular - 将模型/实体/对象添加到 Angular 2 中的 NgModule

TypeScript:我可以声明一个接口(interface)可以有任意数量的进一步属性吗?

twitter-bootstrap - Bootstrap 风格不适用于 Angular2 组件

intellij-idea - 如何在 IntelliJ IDEA 中运行单个 Jasmine 测试

angularjs - 如何在AngularJS中调试“未定义”的 "Cannot read property '模块?