Angular 2单元测试:如何覆盖@Inject('Window')private _window:Window构造函数依赖?

时间:2016-12-14 17:36:50

标签: unit-testing angular

我尝试了以下方法,例如:

beforeEach(async(() => {

    TestBed.configureTestingModule({
        declarations: [CmpBeingTested],
        providers: [ 
            {provide: window, useValue: window}

        ]
    });
}));

我无法覆盖CmpBeingTested构造函数:

constructor(@Inject('Window') private _window: Window) { }

我一直收到错误

没有Window的提供者!

1 个答案:

答案 0 :(得分:0)

  beforeEach(async(() => {

        let windowMock: Window = { } as any;

        TestBed.configureTestingModule({
            declarations: [CmpBeingTested],
            providers: [ 
                { provide: 'Window', useFactory: (() => { return  windowMock; }) }

            ]
        });
    }));