Angular 角度单元测试注入参数

Angular 角度单元测试注入参数,angular,unit-testing,karma-jasmine,inject,Angular,Unit Testing,Karma Jasmine,Inject,我正在尝试使用inject设置单元测试。但我不知道如何设置参数 正在测试的类(auth.service.ts)的构造函数是: 单元测试类(auth.service.spec.ts) 您需要使用HttpClientTestingModule来测试HttpClient在您的服务中的使用情况 为令牌存储使用假值,否则单元测试将成为集成测试 另见 您需要使用HttpClientTestingModule来测试HttpClient在您的服务中的使用情况 为令牌存储使用假值,否则单元测试将成为集成测试 另见

我正在尝试使用inject设置单元测试。但我不知道如何设置参数

正在测试的类(auth.service.ts)的构造函数是:

单元测试类(auth.service.spec.ts)

  • 您需要使用
    HttpClientTestingModule
    来测试
    HttpClient
    在您的服务中的使用情况
  • 令牌存储
    使用假值,否则单元测试将成为集成测试
  • 另见

  • 您需要使用
    HttpClientTestingModule
    来测试
    HttpClient
    在您的服务中的使用情况
  • 令牌存储
    使用假值,否则单元测试将成为集成测试
  • 另见


    标签用于(2+)和(1x)。使用正确的标签将有助于让开发人员了解您的问题,从而更好地回答您的问题。一些有用的东西标签用于(2+)和标签用于(1x)。使用正确的标记将有助于让开发人员了解您的问题,从而更好地回答您的问题。我得到以下一些有用的错误:错误:StaticInjectorError(DynamicTestModule)[AuthService->HttpClient]:错误属性:对象({NgteTokenPath:null,NgteTokenPath:['AuthService',Function]})我得到了以下错误:错误:StaticInjectorError(DynamicTestModule)[AuthService->HttpClient]:错误属性:对象({ngTestOkenPath:null,ngTokenPath:['AuthService',Function]})
    constructor(private http : HttpClient, private token: TokenStorage) {}
    
    import { TestBed, inject } from '@angular/core/testing';
    import { AuthService } from './auth.service';
    import { HttpClient, HttpHandler, HttpClientModule } from '@angular/common/http';
    import { TokenStorage } from './token.storage';
    
    describe('AuthService', () => {
        beforeEach(() => {
            TestBed.configureTestingModule({
                providers: [AuthService, HttpClient, HttpHandler, HttpClientModule, TokenStorage]
            });
        });
        it('should be created', inject([AuthService], (service: AuthService) => {
            expect(service).toBeTruthy();
        }));
    });
    
    import { TestBed, inject, getTestBed } from '@angular/core/testing';
    import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
    import { AuthService } from './auth.service';
    import { TokenStorage } from './token.storage';
    
    describe('AuthService', () => {
      let injector: TestBed;
      let service: AuthService;
      let httpMock: HttpTestingController;
    
      beforeEach(() => {
        TestBed.configureTestingModule({
          imports: [HttpClientTestingModule],
          providers: [
            AuthService,
            { provide: TokenStorage, useValue: {} }
          ]
        });
        injector = getTestBed();
        service = injector.get(AuthService);
        httpMock = injector.get(HttpTestingController);
      });
    
      afterEach(() => {
        httpMock.verify();
      });
    
      it('should be created', () => {
        expect(service).toBeTruthy();
      });
    });