How can i mock angular-auth-oidc-client to return some fake token using karma-jasmine. Below is the code that i need to write a unit test case.
getToken() {
return this.oidcSecurityService.getToken();
}
How can i mock angular-auth-oidc-client to return some fake token using karma-jasmine. Below is the code that i need to write a unit test case.
getToken() {
return this.oidcSecurityService.getToken();
}
Here is my article which covers all such basic testing scenarios to start with. There is another article which specifically talks about this case. Feel free to provide your feedback
You'll need to create a stub
which will mock the behavior of oidcSecurityService
,
export class OidcSecurityServiceStub{
getToken(){
return 'some_token_eVbnasdQ324';
}
// similarly mock other methods "oidcSecurityService" as per the component requirement
}
then in spec
file, use useClass
as below in TestBed
:
TestBed.configureTestingModule({
declarations: [ WhateverComponent],
providers: [ {provide: OidcSecurityService(or whatever the name is), useClass: OidcSecurityServiceStub} ]
});
I'm assuming you're testing a component. You could try the method mentioned here: https://angular.io/guide/testing#final-setup-and-tests.
Edit and excerpt from the site:
let userServiceStub: Partial<UserService>;
beforeEach(() => {
// stub UserService for test purposes
userServiceStub = {
isLoggedIn: true,
user: { name: 'Test User'}
};
TestBed.configureTestingModule({
declarations: [ WelcomeComponent ],
providers: [ {provide: UserService, useValue: userServiceStub } ]
});
fixture = TestBed.createComponent(WelcomeComponent);
comp = fixture.componentInstance;
// UserService from the root injector
userService = TestBed.get(UserService);
// get the "welcome" element by CSS selector (e.g., by class name)
el = fixture.nativeElement.querySelector('.welcome');
});