I am testing a simple service. The service uses 2 values from another service.
Basically, I would like to test those 2 values : isLogged = false and isLogged = true.
Is it possible to just change the value of an injected service, or do I need to do something else ? (which I don't know, so if you could lead me on the way, I'd appreciate it).
Here is my code for testing :
EDIT I have found a solution to my problem. You need to inject the provider to the inject parameters, and you can change its properties as you want after.
import { TestBed, async, inject } from '@angular/core/testing';
import { AuthGuardService } from './authguard.service';
import { Router } from '@angular/router';
import { AuthService } from './auth.service';
describe('AuthguardService', () => {
let calledUrl = '/logged/main/last';
let authServiceStub = {
isLogged: false,
redirectUrl: calledUrl
};
class RouterStub {
navigate(url: string) { return url; }
};
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
AuthGuardService,
{ provide: AuthService, useValue: authServiceStub },
{ provide: Router, useClass: RouterStub },
]
});
});
it('should ...', inject([AuthGuardService, Router], (service: AuthGuardService, router: Router) => {
let spy = spyOn(router, 'navigate');
service.checkLoginState(calledUrl);
expect(spy).toHaveBeenCalledWith(['/login']);
}));
});
isLogged
is false, it redirects to'login'
and if it's true, to the parameter given to the function checkLoginState – Dextrality