My Angular application utilizes the oidc-client
UserManager class for managing OAuth authentication.
The service I have is structured like this:
export class AuthService {
private userManager: UserManager
private readonly configService: ConfigService;
constructor(configService: ConfigService) {
this.configService = configService;
}
...
async load(): Promise<any> {
config = this.configService.getConfig();
this.userManager = new UserManager(config);
const user = await this.userManager.getUser();
...
Below is the setup of my spec file:
beforeEach(() => {
const spy = jasmine.createSpyObj('UserManager', ['getUser']);
spy.getUser.and.returnValue(mockUser);
const configSpy = jasmine.createSpyObj('ConfigService', ['getConfig']);
configSpy.getConfig.and.returnValue(mockConfig);
TestBed.configureTestingModule({
providers: [
AuthenticationService,
{ provide: UserManager, useValue: spy },
{ provide: AppConfigService, useValue: configSpy }
]
});
authService = TestBed.inject(AuthenticationService);
appConfigSpy = TestBed.inject(ConfigService) as jasmine.SpyObj<ConfigService>;
userManagerSpy = TestBed.inject(UserManager) as jasmine.SpyObj<UserManager>;
});
...and here is my initial test case :
it('should initialize the user manager', async () => {
// arrange
userManagerSpy.getUser.and.resolveTo(mockUser);
appConfigSpy.getConfig.and.returnValue(mockConfig);
// act
await authService.load();
// assert
expect(userManagerSpy).toHaveBeenCalled();
});
When running tests, I encounter a 404 error which leads me to believe that new UserManager(config)
and/or this.userManager.getUser()
are attempting to make HTTP requests instead of returning mock values.
How can I spyOn userManager
and mock the return value from getUser()
?
I thought that the TestBed.configureTestModule providers were meant for setting up services injected into the service, not for members of the service itself.