我如何模拟 Angular-auth-oidc-client 以使用 karma-jasmine 返回一些假 token 。下面是我需要编写单元测试用例的代码。
getToken() {
return this.oidcSecurityService.getToken();
}
最佳答案
Here is my article which covers all such basic testing scenarios首先。 There is another article which specifically talks about this case 。请随时提供您的反馈
您需要创建一个 stub
来模拟oidcSecurityService
的行为,
export class OidcSecurityServiceStub{
getToken(){
return 'some_token_eVbnasdQ324';
}
// similarly mock other methods "oidcSecurityService" as per the component requirement
}
然后在spec
文件中,使用useClass
,如下在TestBed
中:
TestBed.configureTestingModule({
declarations: [ WhateverComponent],
providers: [ {provide: OidcSecurityService(or whatever the name is), useClass: OidcSecurityServiceStub} ]
});
关于typescript - 我如何模拟 angular-auth-oidc 客户端以返回假数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57199468/