We need to implement an AuthServiceFake
so that our unit tests pass and use a pattern similar to commonTestingModules
mentioned in Chapter 12, Create a Router-First Line-of-Business App, to conveniently provider this fake across our spec files.
To ensure that our fake will have the same public functions and properties as the actual AuthService
, let's first start with creating an interface:
- Add
IAuthService
toauth.service.ts
src/app/auth/auth.service.ts
export interface IAuthService {
authStatus: BehaviorSubject<IAuthStatus>
login(email: string, password: string): Observable<IAuthStatus>
logout()
getToken(): string
}
- Make sure
AuthService
implements the interface - Export
defaultAuthStatus
for reuse
src/app/auth/auth.service.ts export const defaultAuthStatus = { isAuthenticated: false, userRole: Role.None, userId: null, } export class AuthService extends CacheService implements IAuthService
Now we can create a fake that...