NullInjectorError: StaticInjectorError[AviorBackendService]: NullInjectorError: No provider for even though the service is provided

NullInjectorError: StaticInjectorError[AviorBackendService]: NullInjectorError: No provider for even though the service is provided

我这里有我的测试代码:

import { TestBed, inject } from '@angular/core/testing';

import { AviorBackendService } from './avior-backend.service';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';

describe('AviorBackendService', () => {
  beforeEach(() => TestBed.configureTestingModule({imports: [HttpClientTestingModule],
    providers: [ AviorBackendService ]}));

  it('should be created', () => {
    const service: AviorBackendService = TestBed.get(AviorBackendService);
    expect(service).toBeTruthy();
  });
});

it('expects service to fetch data with proper sorting', () => {
      const service: AviorBackendService = TestBed.get(AviorBackendService);
      // tslint:disable-next-line: prefer-const
      let httpMock: HttpTestingController;
      service.getUserCollection().subscribe(data => {
      expect(data.length).toBe(7);
      const req = httpMock.expectOne('http://localhost:3000/users');
      expect(req.request.method).toEqual('GET');      // Then we set the fake data to be returned by the mock
      req.flush({firstname: 'Chad'});
      });
    });

it('should create the Preferences Service', inject([AviorBackendService], (service: AviorBackendService) => {
          expect(service).toBeTruthy();
        }));

如您所见,提供了 AviorBackendService,但第二次和第三次测试仍会抛出错误。如何解决这个问题?我尝试将其更改为声明,但这给了我其他新错误,因此它似乎不是一个合适的解决方案。

您只提供单次测试服务,即第一次测试。 beforeEach 块的范围在 describe 块内。这意味着 describe 块内的所有 it 块都可以访问提供的服务,但 none 块外。

这是应该可以使用的更新版本。

import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
import {inject, TestBed} from '@angular/core/testing';
import {AviorBackendService} from './avior-backend.service';

describe('AviorBackendService', () => {
  beforeEach(() => TestBed.configureTestingModule({
    imports: [HttpClientTestingModule],
    providers: [AviorBackendService],
  }));

  it('should be created', () => {
    const service: AviorBackendService = TestBed.get(AviorBackendService);
    expect(service).toBeTruthy();
  });

  it('expects service to fetch data with proper sorting', () => {
    const service: AviorBackendService = TestBed.get(AviorBackendService);
    // tslint:disable-next-line: prefer-const
    let httpMock: HttpTestingController;
    service.getUserCollection().subscribe(data => {
      expect(data.length).toBe(7);
      const req = httpMock.expectOne('http://localhost:3000/users');
      expect(req.request.method).toEqual('GET');      // Then we set the fake data to be returned by the mock
      req.flush({firstname: 'Chad'});
    });
  });

  it('should create the Preferences Service', inject([AviorBackendService], (service: AviorBackendService) => {
    expect(service).toBeTruthy();
  }));
});

提供给 beforeEach 块的方法将在每个 it 块之前 运行 并使用您之前缺少的服务初始化您的 TestBed。