我已经开始为一个Angular 9 应用编写测试。我遵循了Jeff Camera在这篇文章中给出的建议,大约在文章的一半处:
如何在TypeScript中显式设置`window`上的新属性?
如果您需要使用import扩展具有自定义类型的window对象,可以使用以下方法:
window.d.ts
import MyInterface from './MyInterface';
declare global { interface Window { propName: MyInterface } }
当我运行ng test时,我没有收到此错误。
这是我针对相关组件的spec.ts文件。
我不确定在哪里应该添加Window的提供者。
如何在TypeScript中显式设置`window`上的新属性?
如果您需要使用import扩展具有自定义类型的window对象,可以使用以下方法:
window.d.ts
import MyInterface from './MyInterface';
declare global { interface Window { propName: MyInterface } }
当我运行ng test时,我没有收到此错误。
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的提供者。