ng run test return error - TypeError:_this.handler.handle不是函数

时间:2018-02-20 13:00:25

标签: angular karma-jasmine angular5 angular-test

我用角度5.2.0创建了c3条形图。一切正常。但我只想使用karma和jasmine与npm run test进行测试。但是我遇到了以下问题。我希望它与rxjs函数有关。但我无法理解错误核心。请帮助任何人解决这个问题。

TypeError: _this.handler.handle is not a function
    at MergeMapSubscriber.project (http://localhost:9876/_karma_webpack_/webpack:/C:/Workspace/datawens-master/node_modules/@angular/common/esm5/http.js:1466:80)
    at MergeMapSubscriber.webpackJsonp.../../../../rxjs/_esm5/operators/mergeMap.js.MergeMapSubscriber._tryNext (http://localhost:9876/_karma_webpack_/webpack:/C:/Workspace/datawens-master/node_modules/rxjs/_esm5/operators/mergeMap.js:128:1)
    at MergeMapSubscriber.webpackJsonp.../../../../rxjs/_esm5/operators/mergeMap.js.MergeMapSubscriber._next (http://localhost:9876/_karma_webpack_/webpack:/C:/Workspace/datawens-master/node_modules/rxjs/_esm5/operators/mergeMap.js:118:1)
    at MergeMapSubscriber.webpackJsonp.../../../../rxjs/_esm5/Subscriber.js.Subscriber.next (http://localhost:9876/_karma_webpack_/webpack:/C:/Workspace/datawens-master/node_modules/rxjs/_esm5/Subscriber.js:92:1)
    at ScalarObservable.webpackJsonp.../../../../rxjs/_esm5/observable/ScalarObservable.js.ScalarObservable._subscribe (http://localhost:9876/_karma_webpack_/webpack:/C:/Workspace/datawens-master/node_modules/rxjs/_esm5/observable/ScalarObservable.js:51:1)
    at ScalarObservable.webpackJsonp.../../../../rxjs/_esm5/Observable.js.Observable._trySubscribe (http://localhost:9876/_karma_webpack_/webpack:/C:/Workspace/datawens-master/node_modules/rxjs/_esm5/Observable.js:172:1)
    at ScalarObservable.webpackJsonp.../../../../rxjs/_esm5/Observable.js.Observable.subscribe (http://localhost:9876/_karma_webpack_/webpack:/C:/Workspace/datawens-master/node_modules/rxjs/_esm5/Observable.js:160:1)
    at MergeMapOperator.webpackJsonp.../../../../rxjs/_esm5/operators/mergeMap.js.MergeMapOperator.call (http://localhost:9876/_karma_webpack_/webpack:/C:/Workspace/datawens-master/node_modules/rxjs/_esm5/operators/mergeMap.js:92:1)
    at Observable.webpackJsonp.../../../../rxjs/_esm5/Observable.js.Observable.subscribe (http://localhost:9876/_karma_webpack_/webpack:/C:/Workspace/datawens-master/node_modules/rxjs/_esm5/Observable.js:157:1)
    at FilterOperator.webpackJsonp.../../../../rxjs/_esm5/operators/filter.js.FilterOperator.call (http://localhost:9876/_karma_webpack_/webpack:/C:/Workspace/datawens-master/node_modules/rxjs/_esm5/operators/filter.js:61:1)

4 个答案:

答案 0 :(得分:4)

我可以通过在app.module.ts中进行以下更改来解决此问题。希望这会有所帮助。



import { HttpClient } from '@angular/common/http';
import { HttpClientModule } from '@angular/common/http';
...
...

@NgModule({
  declarations: [
    MyApp,
    HomePage,
    CameraPage,
  ],
  imports: [
    BrowserModule,
//  HttpClient,
   HttpClientModule,




答案 1 :(得分:2)

我在尝试将测试转换为使用HttpTestingController时遇到了这个问题。 This答案只是解决问题并将其打印到控制台。对于下面的解决方案,我仍然有很多地方仍然无法理解,但是希望它可以对某人有所帮助。

应用程序中的问题是我在HttpHandler列表中提供了providers。这导致_this.handler成为堆栈跟踪中HttpHandler中有问题的行上的http.js

var /** @type {?} */ events$ = concatMap.call(of(req), function (req) { return _this.handler.handle(req); });

我从HttpHandler的提供者中删除了TestBed.configureTestModule,然后_this.handler是一个具有适当HttpInterceptorHandler功能的handle

答案 2 :(得分:0)

似乎没有错误处理的订阅可能会导致这种情况,如here所述。 该链接中的答案提供了以下示例:

"languageId IN languageids"

答案 3 :(得分:-1)

服务调用失败typoerror this.handler.handle不是函数或

typeerror:此处理程序句柄不是函数

如法案所述,我从提供商列表中删除了HttpClient和HttpHandler并添加了httpclientmodule问题将解决