Angular 如何使用Jasmine模拟构造函数中使用的配置对象的值

Angular 如何使用Jasmine模拟构造函数中使用的配置对象的值,angular,jasmine,Angular,Jasmine,我最近开始使用Jasmine来测试一个角度应用程序,虽然大多数测试都运行良好,但我在使用一个特定的应用程序时遇到了问题 这是AppComponent的测试,如下所示: 应用程序组件.ts 从'@angular/core'导入{Component,OnDestroy}; 从“ng2空闲内核”导入{Idle,DEFAULT_INTERRUPTSOURCES}; 从'@ngxs/Store'导入{Store}; 从'angular-oauth2-oidc'导入{OAuthService,JwksVal

我最近开始使用Jasmine来测试一个角度应用程序,虽然大多数测试都运行良好,但我在使用一个特定的应用程序时遇到了问题

这是AppComponent的测试,如下所示:

应用程序组件.ts

从'@angular/core'导入{Component,OnDestroy};
从“ng2空闲内核”导入{Idle,DEFAULT_INTERRUPTSOURCES};
从'@ngxs/Store'导入{Store};
从'angular-oauth2-oidc'导入{OAuthService,JwksValidationHandler};
进口{
authConfig,
登录成功,
CriticalErrorHandler,
“/core”中的CriticalLogoutRequest};
@组成部分({
选择器:“我的应用程序”,
templateUrl:“./app.component.html”,
样式URL:['./app.component.scss']
})
导出类AppComponent{
IDLState='未启动';
timedOut=false;
lastPing?:日期=null;
//在这里保留“CriticalErrorHandler”很重要,因为它是一个可注入的“服务”,必须
//在应用程序开始时实例化
建造师(
私人闲置:闲置,
私家店,
私人criticalErrorHandler:criticalErrorHandler,
专用oauthService:oauthService){
//空闲时间
这个.idle.setIdle(3600);
此.idle.setTimeout(30);
this.idle.setInterrupts(默认的中断源);
this.idle.onIdleStart.subscribe(()=>{
this.store.dispatch(new CriticalLogoutRequest());
});
this.login();
}
私有登录(){
this.oauthService.configure(authConfig);
this.oauthService.tokenValidationHandler=新的JwksValidationHandler();
this.oauthService.loadDiscoveryDocumentAndLogin({
已收到:()=>{
this.oauthService.setupAutomaticSilentRefresh();
this.store.dispatch(new loginsucess());
}
});
}
}
可以看出,构造函数调用login()函数,该函数反过来使用名为authConfig的配置对象,如下所示:

auth.config.ts

从“angular-oauth2-oidc”导入{AuthConfig};
从“环境/环境”导入{environment};
导出常量authConfig:authConfig={
silentRefreshRedirectUri:window.location.origin+'/assets/silent refresh.html',
颁发者:environment.endpoints.identity,
重定向URI:window.location.origin+'/index.html',
clientId:'ID',
作用域:“openid用户权限My.WebApp”,
responseType:“id\u令牌”,
requireHttps:environment.requireHttps,
SessionCheckEnabled:true
};
这将调用另一个名为environment的配置对象,该对象的requireHttps设置为true。所以当我做一个简单的create测试时,就像这样:

应用组件规范ts

从'@angular/core'导入{CUSTOM_ELEMENTS_SCHEMA};
从'@angular/core/testing'导入{async,TestBed,ComponentFixture};
从'@angular/router'导入{RouterModule};
从'priming/priming'导入{SharedModule};
从“./app.component”导入{AppComponent};
从“./core/core.module”导入{CoreModule};
fdescribe('AppComponent',()=>{
let组件:AppComponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
进口:[
核心模块,
共享模块,
RouterModule.forRoot([])
],
声明:[AppComponent],
模式:[自定义元素\u模式]
}).compileComponents();
}));
它('应该创建应用',异步(()=>{
fixture=TestBed.createComponent(AppComponent);
组件=fixture.componentInstance;
expect(component.toBeTruthy();
}));
});

我得到一个“发卡机构必须使用https,或者属性requireHttps必须允许http的配置值”。所以我想做的是,在测试范围内,将config对象中的requireHttps属性更改为false。但我真的不知道该怎么做。。。感谢您的帮助

如果将
AuthConfig
作为参数传递给组件的
login()
,该怎么办?这将允许您在单元测试中再次模拟
AuthConfig

应用程序组件.ts

从“/core”导入{authConfig};
...
this.login(authConfig);
...
私有登录(_authConfig:authConfig){
这个.oauthService.configure(\u authConfig);
this.oauthService.tokenValidationHandler=新的JwksValidationHandler();
this.oauthService.loadDiscoveryDocumentAndLogin({
onTokenReceived:()=>{
this.oauthService.setupAutomaticSilentRefresh();
this.store.dispatch(new loginsucess());
}
});
}
在测试套件中,您将模拟
AuthConfig
对象

应用组件规范ts


在每个(()=>async(()=>{
...
让mockAuthConfig:AuthConfig;
...
}))
在每个之前(()=>{
...
mockAuthConfig={
silentRefreshRedirectUri:window.location.origin+'/assets/silent refresh.html',
颁发者:environment.endpoints.identity,
重定向URI:window.location.origin+'/index.html',
clientId:'ID',
作用域:“openid用户权限My.WebApp”,
responseType:“id\u令牌”,
requireHttps:false,//{
login(mockAuthConfig);
//你的测试在这里
//期望(…)。成为(…);
}))
本质上,在app.component.ts中调用login()将使用预定义并导出的
authConfig
。但是,在单元测试用例中,您将模拟(重新创建)一个新的
authConfig
对象,该对象具有不同的属性,然后通过
component.login将其传递到
login()
(mockAuthConfig)

希望这对你有用<