SearchService应该创建失败。 TypeError:无法读取null的属性“ ngModule”

时间:2020-10-13 21:30:31

标签: angular typescript unit-testing web-testing

我正在尝试对网站的我的模块目录中的服务进行单元测试。但是,当我npm测试它时,它给了我这个错误:

Chrome 86.0.4240.80 (Mac OS 10.15.7) SearchService should be created FAILED
    TypeError: Cannot read property 'ngModule' of null
        at isModuleWithProviders (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:26155:1)
        at expandModuleWithProviders (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:26149:1)
        at <Jasmine>
        at Function.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:25805:1)
        at getNgModuleDef (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:1842:1)
        at verifySemanticsOfNgModuleDef (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:25860:1)
        at Function.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:25828:1)
        at R3TestBedCompiler.applyProviderOverridesToModule (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:1331:1)
        at R3TestBedCompiler.compileTestModule (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:1576:1)
        at R3TestBedCompiler.finalize (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:1185:1)
    Error: Expected undefined to be truthy.
        at <Jasmine>
        at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/modules/dashboard/services/search.service.spec.ts:21:21)
Chrome 86.0.4240.80 (Mac OS 10.15.7): Executed 1 of 1 (1 FAILED) (1.514 secs / 1.5 secs)
TOTAL: 1 FAILED, 0 SUCCESS

我的search.service.spec.ts几乎是一个骨架:

import { TestBed } from '@angular/core/testing';
import { SearchService } from './search.service';
import { HttpClientModule } from '@angular/common/http';

describe('SearchService', () => {
  let service: SearchService;
  let mockSearchService = jasmine.createSpyObj('searchService', ['search']);
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers:[
        {provide: SearchService, useValue: mockSearchService}
      ],
      imports: [HttpClientModule],
    });
    service = TestBed.inject(SearchService);
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });
});

module.ts也很裸露。根据Angular文档,服务应该放在NgModule的provider部分中,我不确定是否应该在顶部导入它。

import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { SharedModule } from '@shared/shared.module';
import { SearchService } from './services';

@NgModule({
    imports: [
        CommonModule,
        FormsModule,
        SharedModule,
        ReactiveFormsModule,
    ],
    declarations: [
    ],
    entryComponents: [
    ],
    providers: [SearchService],
    exports: [],
})

export class DashBoardModule {}

任何帮助将不胜感激。

0 个答案:

没有答案