I'm trying to test a service in Angular2. In the service, I'm using window.location and window.location.hash several times. After reading some other questions on Stackoverflow, I've decided to inject window in the service rather than using the global window directly.
@Injectable()
export class AuthService {
constructor(private router: Router, private window: Window) { }
}
For that I've registered window in app.module.ts
:
providers: [
AuthService,
{
provide: Window,
useValue: window
},
OtherService
]
Now in my test, I'm trying to mock window in a similar way to this answer: https://stackoverflow.com/a/37176904
beforeEach(() => {
routerStub = {
navigate: jasmine.createSpy('navigate')
};
mockWindow = <any> { location: <any> {hash: 'testhash'}};
TestBed.configureTestingModule({
providers: [
AuthService,
{ provide: Router, useValue: routerStub },
{ provide: Window, useValue: mockWindow }
]
});
authService = TestBed.get(AuthService);
});
The error I get is the following: 'Error: Can't resolve all parameters for AuthService: ([object Object], ?)'
So while injecting a mock router works perfectly fine, injecting a mock window doesn't seem to work. Can anyone help me with this? Is there anything I'm doing wrong? I haven't been able to find a solution that works for me.