Testing - Can't resolve all parameters for (ClassName)

前端 未结 4 1200
清歌不尽
清歌不尽 2020-11-30 11:04

Context

I created an ApiService class to be able to handle our custom API queries, while using our own serializer + other features.

ApiSer

4条回答
  •  离开以前
    2020-11-30 11:38

    [JEST and ANGULAR]

    Also, the problem may occur when you use an external module and you do not import it but use it on your service.

    Ex:

    import { TestBed } from '@angular/core/testing';
    import  from '@ngx-translate/core';
    
    import { SettingsService } from '../../../app/core/services/settings/settings.service';
    
    
    describe('SettingsService', () => {
      let service: SettingsService;
    
      beforeAll(() => {
        TestBed.configureTestingModule({
          providers: [
            SettingsService,
            
          ]
        });
        service = TestBed.inject(SettingsService);
      });
    
      it('should be created', () => {
        expect(service).toBeTruthy();
      });
    
    });
    

    Errors will get you nowhere ... But, if you do that this way:

    import { TestBed } from '@angular/core/testing';
    
    import { TranslateModule } from '@ngx-translate/core';
    
    import { SettingsService } from '../../../app/core/services/settings/settings.service';
    
    
    describe('SettingsService', () => {
      let service: SettingsService;
    
      beforeAll(() => {
        TestBed.configureTestingModule({
          imports: [TranslateModule.forRoot()], <---
          providers: [
            SettingsService
          ]
        });
        service = TestBed.inject(SettingsService);
      });
    
      it('should be created', () => {
        expect(service).toBeTruthy();
      });
    
    });
    

    Problem disappears.

提交回复
热议问题