Unit Testing/mocking Window properties in Angular2 (TypeScript)

前端 未结 4 1680
醉酒成梦
醉酒成梦 2020-12-17 09:23

I\'m building some unit tests for a service in Angular2.

Within my Service I have the following code:

var hash: string; hash = this.window.location.h

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-17 09:31

    After RC4 method provide() its depracated, so the way to handle this after RC4 is:

      let myMockWindow: Window;
    
      beforeEach(() => {
        myMockWindow =  { location:  {hash: 'WAOW-MOCK-HASH'}};
        addProviders([SomeService, {provide: Window, useValue: myMockWindow}]);
      });
    

    It take me a while to figure it out, how it works.

提交回复
热议问题