【问题标题】:getting error Subscribe is not a function in jasmine收到错误订阅不是茉莉花中的功能
【发布时间】:2021-05-26 09:24:37
【问题描述】:

我收到错误,订阅不是角度单元测试中的功能

这是我在组件中使用的调用

this.myService.employees.subscribe(emp => this.emp = emp);

当我为此创建一个模拟服务来测试上面的代码出错时。

谁能建议我如何测试上述 observable。

我想在this.myService.employees.subscribe(emp => this.emp = emp); 订阅组件时发送模拟数据

员工BehaviorSubject可观察的

【问题讨论】:

  • 你的模拟服务是什么样的?

标签: angular unit-testing jasmine karma-jasmine


【解决方案1】:

您能分享一下您在测试中如何配置服务的代码吗?

通常你应该这样做:https://ng-mocks.sudo.eu/extra/mock-observables

最好的选择是使用测试库,例如ng-mocks

beforeEach(() => MockBuilder(MyComponent, MyService));

it('test', () => {
  const employees$ = new Subject();
  MockInstance(MyService, 'employees', employees$);

  const fixture = MockRender(MyComponent);
  employees$.next([]);
  // some assertions.
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-15
    • 2014-12-12
    • 2017-08-12
    相关资源
    最近更新 更多