Angular InjectionToken自定义HTTP配置没有提供程序
我不熟悉因果报应和茉莉花,所以如果这听起来很愚蠢,请原谅我。我有以下代码,我想用它做的是找出下一步需要做什么。我已经在Angular InjectionToken自定义HTTP配置没有提供程序,angular,typescript,unit-testing,karma-jasmine,Angular,Typescript,Unit Testing,Karma Jasmine,我不熟悉因果报应和茉莉花,所以如果这听起来很愚蠢,请原谅我。我有以下代码,我想用它做的是找出下一步需要做什么。我已经在hello HTTP.service.ts中的构造函数中注入了自定义\u HTTP\u CONFIG,我找不到如何实现这一点的教程,手动注入依赖项,这就是我认为错误消息所抱怨的 测试规范ts 你好http.service.ts 因果报应:错误 您可以尝试以下操作:- let svc: HelloHttpService; beforeEach(inject([HelloH
hello HTTP.service.ts
中的构造函数中注入了自定义\u HTTP\u CONFIG
,我找不到如何实现这一点的教程,手动注入依赖项,这就是我认为错误消息所抱怨的
测试规范ts
你好http.service.ts
因果报应:错误
您可以尝试以下操作:-
let svc: HelloHttpService;
beforeEach(inject([HelloHttpService], (serviceArg: HelloHttpService) => {
svc = serviceArg;
}));
试着这样做:
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)我假设它需要所有的依赖项。根据您的方法,我应该只在需要时注入服务,但我会在每次测试之前运行它?谢谢您的建议。我以前也尝试过这种方法,但我无法让它工作:\。可能有一些基本的东西我错过了,无法解决。您是否对async
进行了相同的尝试?不,我们可以将async与injector一起使用吗?我没见过有人用过它。
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)
let svc: HelloHttpService;
beforeEach(inject([HelloHttpService], (serviceArg: HelloHttpService) => {
svc = serviceArg;
}));
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();
}));
});