Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i mock angular-auth-oidc client to return fake data

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();
}
like image 382
Aravind Avatar asked Oct 31 '25 04:10

Aravind


1 Answers

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} ]
  });

like image 62
Shashank Vivek Avatar answered Nov 02 '25 17:11

Shashank Vivek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!