繁体   English   中英

Angular 9 测试错误 NullInjectorError: No provider for Window

[英]Angular 9 test error NullInjectorError: No provider for Window

我已经开始为 Angular 9 应用程序编写测试。 我在这篇文章的一半左右遵循了 Jeff Camera 给出的建议:

如何在 TypeScript 中的“window”上显式设置新属性?

如果您需要使用需要使用导入的自定义类型来扩展 window object,您可以使用以下方法:

window.d.ts

从'./MyInterface'导入MyInterface;

声明全局 { 接口 Window { propName: MyInterface } }

运行 ng test 时没有收到此错误。

NullInjectorError: R3InjectorError(DynamicTestModule)[AdvancedsearchService -> AuthService -> Window -> Window]: 
  NullInjectorError: No provider for Window!

这是我对相关组件的规范

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 应该相同

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM