【问题标题】:No provider for InjectionToken Custom HTTP ConfigInjectionToken 自定义 HTTP 配置没有提供程序
【发布时间】:2019-01-29 17:20:36
【问题描述】:

我是 karma 和 jasmine 的新手,如果这听起来很愚蠢,请原谅我。我有以下这些代码,我想用它做什么,就是找出我接下来需要做什么。我在hello-http.service.ts 的构造函数中注入了CUSTOM_HTTP_CONFIG,我找不到如何实现这一点的教程,手动注入依赖项,这就是我认为错误消息抱怨的原因。

test.spec.ts

beforeEach(async(() => {
 TestBed.configureTestingModule({
  declaration: [...],
  imports[RouterTestingModule, ...],
  providers: [HelloHttpService, ...],
 });
});

hello-http.service.ts

constructor(
 @Inject(CUSTOM_HTTP_CONFIG) protect config: CustomHttpParams,
 ...
) {
 super(config, http);
}

因果报应:错误

Failed: Uncaught (in promise): Error: StaticInjectorError(DynamicTestModule)[HelloHttpService -> InjectionToken Custom HTTP Config]: 
  StaticInjectorError(Platform: core)[HelloHttpService -> InjectionToken Custom HTTP Config]: 
    NullInjectorError: No provider for InjectionToken Custom HTTP Config!
Error: StaticInjectorError(DynamicTestModule)[HelloHttpService -> InjectionToken Custom HTTP Config]: 
  StaticInjectorError(Platform: core)[HelloHttpService -> InjectionToken Custom HTTP Config]: 
    NullInjectorError: No provider for InjectionToken Custom HTTP Config!
    at _NullInjector.webpackJsonp../node_modules/@angular/core/esm5/core.js._NullInjector.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/esm5/core.js:1003:1)
    at resolveToken (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/esm5/core.js:1301:1)

【问题讨论】:

    标签: angular typescript unit-testing karma-jasmine


    【解决方案1】:

    试试这样的:

    import { TestBed, inject } from '@angular/core/testing';
    
    import { HelloHttpService } from './http.service';
    
    describe('HttpService', () => {
        let service : HelloHttpService;
    
        // mock the service, there we no need to pull in any dependency or need to declare constant for the injector aka CUSTOM_HTTP_CONFIG
        const mockHelloHttpService() = { } as HelloHttpService;
    
        beforeEach(() => {
            TestBed.configureTestingModule({
                providers: [{
                 provide: HelloHttpService,
                 useValue: mockHelloHttpService, // must do this, or it will try to find the injector token in the custom http service
                 // which the reason why this error occurred.
             }],
            });
        });
    it('should be created', inject([HelloHttpService], (service: HelloHttpService) => {
        expect(service).toBeTruthy();
    }));
    });
    

    【讨论】:

    • 这是我之前尝试过的方法之一,它在fixture = TestBed.createComponent(LogoutComponent); 这一行抱怨,我认为它需要所有依赖项。根据您的方法,我应该只在需要时注入服务,但我在每次测试之前运行它?
    【解决方案2】:

    您可以尝试以下方法:-

    let svc: HelloHttpService;    
    
    beforeEach(inject([HelloHttpService], (serviceArg: HelloHttpService) => {
        svc = serviceArg;
    }));
    

    【讨论】:

    • 感谢您的建议。我之前也尝试过这种方法,但我无法让它工作:\。可能有一些基本的东西我错过了并且无法解决。
    • 你对async也试过了吗?
    • 不,我们可以将异步与注入器一起使用吗?我没看到有人用过。
    猜你喜欢
    • 2021-09-08
    • 2019-04-18
    • 2022-08-02
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多