Currently, I am in the process of creating a test case for my authentication service outlined below.
AuthService.ts
import {Subject} from 'rxjs';
import {User} from './user.model';
import {AuthData} from './auth-data.model';
import {Injectable} from '@angular/core';
import {Router} from '@angular/router';
@Injectable()
export class AuthService {
authChange = new Subject<boolean>();
private user: User;
constructor(private router: Router) {
}
registerUser(authData: AuthData) {
this.user = {
email: authData.email,
userId: Math.round(Math.random() * 10000).toString()
};
this.authSucessfully();
}
login(authData: AuthData) {
this.user = {
email: authData.email,
userId: Math.round(Math.random() * 10000).toString()
};
this.authSucessfully();
}
logout() {
this.user = null;
this.authChange.next(false);
this.router.navigate(['/login']);
}
getUser() {
return {...this.user};
}
isAuth() {
return this.user !== null;
}
private authSucessfully() {
this.authChange.next(true);
this.router.navigate(['/training']);
}
}
During testing, when calling the `isAuth` method, I encountered a compile-time error. Refer to the attached compile-error.png https://i.sstatic.net/mWI7E.png
The error arises from failing to pass a parameter into the method. However, since the AuthService class constructor includes the Router class, I attempted to provide it with eight null parameters, resulting in further errors. Could someone assist me in figuring out how to effectively test the methods within the AuthService class considering the presence of a Router parameter in the constructor?