Search code examples
typescriptangular6karma-jasmineauth0angular-auth-oidc-client

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

Solution

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