[英]Error in Angular Test using Karma: Uncaught (in promise): Error: No provider for SessionUtil
I am trying to write some tests for my Angular application. 我正在尝试为Angular应用程序编写一些测试。 I am writing my tests in Jasmin / Karma / PhantomJS and I keep getting an error which makes all my tests fail, this is Error: No provider for SessionUtil!
我正在用Jasmin / Karma / PhantomJS编写测试,但是不断出现错误,这使我的所有测试均失败,这是Error: No provider for SessionUtil!
SessionUtil is a Utility Class / Service that has numerous public methods. SessionUtil是一种实用程序类/服务,具有许多公共方法。 in my main.component.ts file I inject it into my construtor like so 在我的main.component.ts文件中,我像这样将其注入到我的构造器中
constructor(@Inject(SessionUtil) private sessionUtil: SessionUtil)
and this is my test file (not all of it, just where I declare everything and import my services, etc) 这是我的测试文件(不是全部,只是我声明所有内容并导入服务的地方,等等)
describe('MainComponent', () => {
let componentFixture: ComponentFixture<MainComponent>;
let instance: any;
let element: any;
let sessionUtil: SessionUtil;
let spyLocalizationsService;
const firstLanguage = 'en-US';
const secondLanguage = 'de-DE';
const unknownLanguage = 'xx-XX';
beforeEach(async(() => {
spyLocalizationsService = sinon.createStubInstance(LocalizationsService);
spyLocalizationsService.changeLanguage.withArgs(firstLanguage, sinon.match.any, sinon.match.any).callsArg(1);
spyLocalizationsService.changeLanguage.withArgs(secondLanguage, sinon.match.any, sinon.match.any).callsArg(1);
spyLocalizationsService.changeLanguage.withArgs(unknownLanguage, sinon.match.any, sinon.match.any).callsArg(2);
spyLocalizationsService.getSupportedLanguages.returns([firstLanguage, secondLanguage]);
(sinon.stub(spyLocalizationsService, 'currentLanguage') as any).get(() => firstLanguage);
// Allows overriding default providers, directives, pipes
TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes([
// some Paths
]),
TranslateI18NextTestingModule.forRoot(),
AuthTestingModule.forRoot(),
NoopAnimationsModule,
MaterialModule
],
declarations: [
MainComponent,
DummyComponent
],
schemas:
[CUSTOM_ELEMENTS_SCHEMA],
providers:
[{provide: LocalizationsService, useValue: spyLocalizationsService},
{provide: MATERIAL_COMPATIBILITY_MODE, useValue: true}]
}).compileComponents().then(() => {
componentFixture = TestBed.createComponent(MainComponent);
element = componentFixture.nativeElement;
instance = componentFixture.componentInstance; // BannerComponent test instance
sessionUtil = componentFixture.debugElement.injector.get(SessionUtil);
});
}));
const isAuthenticated = () => Promise.resolve({
isAuthenticated: true,
username: 'Max Musterman'
});
describe('on init', () => {
beforeEach(async(() => {
sinon.stub(sessionUtil, 'isAuthenticated').returns(isAuthenticated());
}));
when I run my tests I just get the following error and none of my tests run, Uncaught (in promise): Error: No provider for SessionUtil!
当我运行测试时,我仅收到以下错误,并且Uncaught (in promise): Error: No provider for SessionUtil!
运行任何测试, Uncaught (in promise): Error: No provider for SessionUtil!
I am obviously doing something wrong. 我显然做错了。 I tried to inject the SessionUtil
in the TestBed.configureTestingModule
object providers
array, but this gave me just as many problems. 我试图将SessionUtil
注入TestBed.configureTestingModule
对象providers
数组中,但这给了我很多问题。 Can anyone see how I am injecting this incorrectly? 谁能看到我是如何错误地注射这种药物的?
Any advice would be appreciated! 任何意见,将不胜感激!
{provide: SessionUtil, useValue: sessionUtil}
而且您需要使用存根启动sessionUtil
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.