简体   繁体   English

Angular5 Karma测试:Rxjs运算符不是函数

[英]Angular5 Karma test: Rxjs operator is not a function

I have a component (MyComponent) that uses mergeMap - it builds, runs and does everything I want. 我有一个使用mergeMap的组件(MyComponent)-它可以构建,运行并完成我想要的一切。 No problems here. 没问题 I removed the real implementation for this example. 我删除了此示例的实际实现。

During the Karma test I get an error thrown 'service.$find(...).mergeMap is not a function' . 在Karma测试期间,我抛出了一个错误'service.$find(...).mergeMap is not a function' What am I doing wrong? 我究竟做错了什么?

import {Observable} from "rxjs/Observable";

export component MyComponent {
  constructor(private service: Service) {
    let observable = service.$find().mergeMap(() => { return Observable.of() });
  }
}

I mock this service for the test: service.mock.ts 我为测试模拟了此服务:service.mock.ts

export class MockService {
  $find() { return Observable.of() }
}

I test this component in mycomponent.spec.ts 我在mycomponent.spec.ts中测试了此组件

describe("MyComponent", () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [{provide: Service, useClass: Mockservice}],
      declarations: [MyComponent]
    })
  }
  // setup and it("should create") ...

}

I tried import {mergeMap} from "rxjs/Operators/mergeMap"; 我尝试import {mergeMap} from "rxjs/Operators/mergeMap"; at several places as well. 在几个地方也是如此。

If you're using Observable patching to add operators you need to add them from rxjs/add/operator/xyz : 如果您使用Observable修补程序添加运算符,则需要从rxjs/add/operator/xyz添加它们:

import 'rxjs/add/operator/mergeMap';

For Observable (or static methods on the Observable class that create new Observables): 对于Observable(或创建新Observable的Observable类上的静态方法):

import 'rxjs/add/observable/of';

Or if this is only for your tests you could import from rxjs like (this loads all patches from rxjs/add as well): 或者,如果这仅用于您的测试,则可以从rxjs导入(例如,这rxjs/addrxjs/add加载所有补丁):

import { Observable } from "rxjs";

Note, that this will include everything so it's not recommended in production. 请注意,这将包括所有内容,因此不建议在生产中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM