Angular/Jasmine:无法读取属性';管道';未定义的

Angular/Jasmine:无法读取属性';管道';未定义的,angular,typescript,unit-testing,rxjs,jasmine,Angular,Typescript,Unit Testing,Rxjs,Jasmine,我试图用Angular 9、Jasmine和RxJS解决一个问题,但没有成功。我的单元测试是由Jasmine运行的,但我有几行代码没有执行 我一直在寻找一些帖子的帮助,但是这些文章都不能提供一个解决我问题的方法,我真的很沮丧 我需要帮助,请:) 课堂测试 description('AuthRefreshAppInterceptor',()=>{ 让accessToken:string; 让端点:字符串; 让authAppServiceMock:AuthAppService; let baseHT

我试图用Angular 9、Jasmine和RxJS解决一个问题,但没有成功。我的单元测试是由Jasmine运行的,但我有几行代码没有执行

我一直在寻找一些帖子的帮助,但是这些文章都不能提供一个解决我问题的方法,我真的很沮丧

我需要帮助,请:)

课堂测试

description('AuthRefreshAppInterceptor',()=>{
让accessToken:string;
让端点:字符串;
让authAppServiceMock:AuthAppService;
let baseHTTPService:baseHTTPService;
让environmentServiceMock:EnvironmentService;
设httpTestingController:httpTestingController;
让sessionStorageServiceMock:SessionStorageService;
在每个之前(()=>{
TestBed.configureTestingModule({
导入:[HttpClientTestingModule],
供应商:[
Scriptografia服务,
BaseHTTPService,
{
提供:AuthAppService,
useValue:jasmine.createSpyObj('AuthAppService',['getNewAuth','saveAuth','createBodyAuth','createBodyAuth']))
},
{
提供:环境服务,
useValue:EnvironmentMock
},
{
提供:HTTP_拦截器,
useClass:AutoRizaoAppInterceptor,
多:是的,
},
{
提供:会话存储服务,
useValue:jasmine.createSpyObj('SessionStorage'服务、['saveItem','getItem','removeItem']))
}
],
});
authAppServiceMock=TestBed.inject(AuthAppService);
baseHTTPService=TestBed.inject(baseHTTPService);
environmentServiceMock=TestBed.inject(EnvironmentService);
httpTestingController=TestBed.inject(httpTestingController);
sessionStorageServiceMock=TestBed.inject(SessionStorageService);
accessToken=faker.finance.bitcoinAddress();
endpoint=faker.internet.domainName();
SessionStorage ServiceMock.getItem=jasmine.createSpy()
.and.returnValue({access_token:accessToken});
});
之后(()=>{
httpTestingController.verify();
authAppServiceMock.obterAutorizacaoApp=jasmine.createSpy()
.和.返回值(可观察的({
访问令牌:“EYJHBGCIOIJSUZI1NIISIMTPZCI6IK5OMXY4YLZSODG2M3G3UNHWTLJHAMCHILJ0EXAIIOIJHDCTQD3QIFQ.EYJJUYMYOJE1OTU5JASIMv46MCTU5OTU5OTUYRJHJJJJJJJJJJJJJJ0D0EVNLC6IKJ0F5F4F9PZUIW9F4F4F4F4F4F4D6MCU6MJJJJJJ0J0J0J0J0J0J0V0V0V0V0V0V0V0V0V0V0V0V0V0V0V0V0V0V0W9V0W9V0W9V0W9V0W9V0W9V0W9V0W9V0W9V0WCspsdU8a8XhwFTgoCDmjSlXKSZKmUGsEaCbXuzSQg7BwD7A9zul0U0VkbF1KTvLIdnmb1noyeOP04dDH',
在:3600,
令牌类型:'承载',
作用域:“IdentityServerApi”
}));
});
它('应该执行一个请求获取并返回一个带有错误401 Unauthorized的HttpErrorResponse',()=>{
spyOn(baseHTTPService,'httpGet')。和.callThrough();
baseHTTPService.httpGet(端点).subscribe({
错误:(httpErrorResponse:httpErrorResponse)=>{
console.log(httpErrorResponse)
expect(httpErrorResponse.status).toEqual(401);
expect(httpErrorResponse.statusText).toEqual('Unauthorized');
expect(httpErrorResponse.message).toContain('Http failure response for');
}
});
const httpRequest=httpTestingController.expectOne(
`${environmentServiceMock.apiGatewayURL}/${endpoint}`
);
flush(null,{status:401,statusText:'Unauthorized'});
期望(baseHTTPService.httpGet).toHaveBeenCalledTimes(1);
expect(httpRequest.request.url).toBe(`${environmentServiceMock.apiGatewayURL}/${endpoint}`);
expect(httpRequest.request.method).toBe('GET');
expect(httpRequest.request.body).toBeNull();
});
...
});
拦截器:测试类

导出类AuthRefreshAppInterceptor{
...
私有getNewAuth(请求:HttpRequest,下一步:HttpHandler){
返回此.authAppService
.getAuthApp()
.烟斗(
//此块未经测试
switchMap((auth:AuthAppModel)=>{
this.authAppService.saveAuth(auth);
返回next.handle(this.addHeaders(request));
}),
//此块未经测试
catchError(()=>next.handle(请求))
);
}
...
}
服务

导出类AuthAppService{
...
公共萨尔瓦拉乌托里扎科(作者:AuthAppModel){
this.sessionStorageService.removietem(this.authKey);
this.sessionStorageService.saveItem(this.authKey,auth);
}
public getAuthApp():可观察{
返回this.httpClient.post(
`${this.environmentService.apiGateway}/identity/token`,
此.createBodyAuth(),
{headers:this.createHeaders()}
);
}
...
}

谢谢

您必须在
jasmine.createSpyObj

 {
   provide: AuthAppService,
   useValue: jasmine.createSpyObj('AuthAppService', ['getAuthApp' 'getNewAuth', 'saveAuth', 'createBodyAuth', 'createBodyAuth']) // add 'getAuthApp' here
 },
对于
成功路径
(开关映射)

对于
故障路径
(catchError)


您必须在
jasmine.createSpyObj

 {
   provide: AuthAppService,
   useValue: jasmine.createSpyObj('AuthAppService', ['getAuthApp' 'getNewAuth', 'saveAuth', 'createBodyAuth', 'createBodyAuth']) // add 'getAuthApp' here
 },
对于
成功路径
(开关映射)

对于
故障路径
(catchError)


你好@AliF50,谢谢你的帮助o/!代码行现在是覆盖范围,但抛出错误:预期没有打开的请求,发现1:GET您有一个针对api/address endpoint的请求,这是否发生在import { throwError } from 'rxjs'; .... // at the beginning of you test mock this authAppServiceMock.getAuthApp.and.returnValue(throwError({})); // mock your throw error here so it goes into catchError .... proceed with your test