Angular 9 test error NullInjectorError: No provider for Window
Angular 9 test error NullInjectorError: No provider for Window
我已经开始为 Angular 9 应用编写测试。我遵循了 Jeff Camera 给出的建议大约一半 post:
How do you explicitly set a new property on `window` in TypeScript?
如果您需要使用需要使用导入的自定义类型扩展 window 对象,您可以使用以下方法:
window.d.ts
import MyInterface from './MyInterface';
declare global {
interface Window {
propName: MyInterface
} }
我在 运行 ng 测试时没有收到此错误。
NullInjectorError: R3InjectorError(DynamicTestModule)[AdvancedsearchService -> AuthService -> Window -> Window]:
NullInjectorError: No provider for Window!
这是我的 spec.ts 相关组件
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { AdvancedSearchComponent } from './advanced-search.component';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { RouterTestingModule } from '@angular/router/testing';
describe('AdvancedSearchComponent', () => {
let component: AdvancedSearchComponent;
let fixture: ComponentFixture<AdvancedSearchComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule, RouterTestingModule],
declarations: [ AdvancedSearchComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AdvancedSearchComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
我不确定应该在哪里添加 Window 的提供商。
您确定已将 Window 添加到您的模块提供商吗?
此错误表明您使用了 window 但它未在您的 module.ts
中定义
示例:
供应商:[
Window
],
AdvancedsearchService AuthService 应该是一样的
我已经开始为 Angular 9 应用编写测试。我遵循了 Jeff Camera 给出的建议大约一半 post:
How do you explicitly set a new property on `window` in TypeScript?
如果您需要使用需要使用导入的自定义类型扩展 window 对象,您可以使用以下方法:
window.d.ts
import MyInterface from './MyInterface';
declare global { interface Window { propName: MyInterface } }
我在 运行 ng 测试时没有收到此错误。
NullInjectorError: R3InjectorError(DynamicTestModule)[AdvancedsearchService -> AuthService -> Window -> Window]:
NullInjectorError: No provider for Window!
这是我的 spec.ts 相关组件
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { AdvancedSearchComponent } from './advanced-search.component';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { RouterTestingModule } from '@angular/router/testing';
describe('AdvancedSearchComponent', () => {
let component: AdvancedSearchComponent;
let fixture: ComponentFixture<AdvancedSearchComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule, RouterTestingModule],
declarations: [ AdvancedSearchComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AdvancedSearchComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
我不确定应该在哪里添加 Window 的提供商。
您确定已将 Window 添加到您的模块提供商吗? 此错误表明您使用了 window 但它未在您的 module.ts
中定义示例:
供应商:[ Window ],
AdvancedsearchService AuthService 应该是一样的