/* tslint:disable:no-unused-variable */
import { TestBed, async, inject } from '@angular/core/testing';
import { AuthGuardService } from './auth-guard.service';
import { VuiAuthService } from './vui-auth.service';
import { AUTH_REDIRECT } from './injection-tokens/injections-tokens';
import { RouterTestingModule } from '@angular/router/testing';
export class FakeVuiAuthServiceFalse {
isLoggedIn(): boolean {
return false;
}
}
export class FakeVuiAuthServiceReturnTrue {
isLoggedIn() {
return true;
}
}
describe('AuthGuard', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule],
providers: [AuthGuardService,
{
provide: AUTH_REDIRECT,
useValue: {
redirectTo: ''
}
},
{ provide: VuiAuthService, useClass: FakeVuiAuthServiceReturnTrue }
],
});
});
it('when user logged in should return true',
inject([AuthGuardService, VuiAuthService],
(service: AuthGuardService, dep: VuiAuthService) => {
spyOn(dep, 'isLoggedIn');
expect(service.canActivate).toBeTruthy();
}));
it('when user not logged in should return false',
inject([AuthGuardService, VuiAuthService],
(service: AuthGuardService, dep: VuiAuthService) => {
spyOn(dep, 'isLoggedIn');
expect(service.canActivate).toBeFalsy();
}));
});
答案 0 :(得分:0)
您尝试像这样使用spyOn。
在单独的测试用例下指定spyOn并返回不同的值。
spyOn(AuthGuardService.prototype, 'isLoggedIn').and.callFake(() => { return true });