Невозможно протестировать Angular http сервис с использованием Jasmine и Karma - PullRequest
0 голосов
/ 02 апреля 2020

Я пытаюсь проверить перехватчик http, используя Жасмин и Карму. Я получаю ошибку ниже:

 × should return same response to a request
  HeadlessChrome 80.0.3987 (Windows 10.0.0)
Error: In this configuration Angular requires Zone.js
    at new NgZone (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:40482:1)
    at R3TestBedCompiler.compileTestModule (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:2357:1)
    at R3TestBedCompiler.finalize (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:1874:1)
    at TestBedRender3.get testModuleRef [as testModuleRef] (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:3245:1)
    at TestBedRender3.inject (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:3102:1)
    at Function.inject (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:2987:1)
    at UserContext.<anonymous> (http://localhost:9877/_karma_webpack_/src/main/resources/public/scripts/sas/fcs/interceptors/test-interceptor.ng.spec.ts:25:30)
    at <Jasmine>

Мой код ниже:

// Http testing module and mocking controller
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';

// Other imports
import { TestBed } from '@angular/core/testing';
import { HttpClient } from '@angular/common/http';

describe('HttpClient testing', () => {
    let httpClient: HttpClient;
    let httpTestingController: HttpTestingController;

    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [ HttpClientTestingModule ]
        });

        // Inject the http service and test controller for each test
        httpClient = TestBed.inject(HttpClient);
        httpTestingController = TestBed.inject(HttpTestingController);
    });
    /// Tests begin ///
    it('Just a simple test', () => {
        expect(1).toEqual(1)
  // Finally, assert that there are no outstanding requests.
  httpTestingController.verify();
});
});

Если я добавлю импорт 'zone.js / dist / zone', я получу ошибку ниже:

FAILED TESTS:
  ViyaManagerHTTPInterceptor
    × should return same response to a request
      HeadlessChrome 80.0.3987 (Windows 10.0.0)
    TypeError: Cannot read property 'ngModule' of null
        at <Jasmine>
        at isModuleWithProviders (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:38897:27)
        at expandModuleWithProviders (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:38887:1)
        at <Jasmine>
        at Function.get (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:38357:1)
        at getNgModuleDef (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:2195:1)
        at verifySemanticsOfNgModuleDef (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:38423:1)
        at Function.get (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:38383:1)
        at R3TestBedCompiler.applyProviderOverridesToModule (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:2079:44)
        at R3TestBedCompiler.compileTestModule (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:2378:1)
        at R3TestBedCompiler.finalize (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:1874:1)

TOTAL: 1 FAILED, 0 SUCCESS

Я не уверен, что происходит. Не входит ли зона js в Angular CLI. Я запускаю тесты, используя команду ng test. Любая помощь очень ценится. Оскорбительные операторы, кажется, TestBed.inject в коде. Если я удаляю их, я не получаю никаких исключений.

...