jasmine模拟值和服务值是不同的



我正在用jasmine测试我的HttpClient post方法:但这是错误的,因为我的业务逻辑方法的返回值是一个Empty数组,但如果我从角度组件运行相同的方法,则该值是等于我的mock数据的填充数组。


it('should call POST /MonitoraggioWS/rs/public/getListaCanaliWSR and return all Channels', () => {
let actualData = {};
let filter: IChannelFilter = {channelCode:'', resourceId:'', resourceType:'QUEUE'};
service.getAllChannels(filter).subscribe(data => actualData = data);
backend.expectOne((req: HttpRequest<any>) => {
return req.url === `/MonitoraggioWS/rs/public/getListaCanaliWSR` && req.method === 'POST'
}, 'Load all channels from /MonitoraggioWS/rs/public/getListaCanaliWSR').flush(channels);
expect(actualData).toEqual(channels);
});

服务是:

const headers = new HttpHeaders().set('Content-Type', 'application/json');
return this.httpClient.post<IChannel[]>(`/MonitoraggioWS/rs/public/getListaCanaliWSR`, JSON.stringify(filter),{headers: headers}).pipe(
map((data: any) => {
let channelList: IChannel[] = [];
if(data && data.channelAdapterList){
data.channelAdapterList.forEach(data =>{
channelList.push({
lastUpdateTimestamp: data.lastUpdateTimestamp,lastUpdateUser: data.lastUpdateUser, external: data.external,
headerExit: data.headerExit, id: data.channelCode, description: data.description, channelCode: data.channelCode,
headerFormat: data.headerFormat, resourceId: data.resourceId, resourceType: data.resourceType, saviour: data.saviour,
version: data.version, organizationLevelTwoDefinition: data.organizationLevelTwoDefinition,
organizationLevelOneDefinition: data.organizationLevelTwoDefinition, mappingOid: data.mappingOid, key: null,
});
});
}
return channelList;
}), catchError( error => {
return throwError( 'MonitoringAPI Something went wrong! '+ error);
})
);
}

我的测试失败了,因为actualDate(我的业务方法的返回值是Array[](,但如果我从角度组件运行该方法,则Array中填充了与我的mock相同的值,那么问题可能是我的业务逻辑服务吗?

我不明白你的全部问题,但我认为你在错误的时间断言。

it('should call POST /MonitoraggioWS/rs/public/getListaCanaliWSR and return all Channels', 
// add this done argument so we can tell Jasmine when we are done with our assertions
(done) => {
let actualData = {};
let filter: IChannelFilter = {channelCode:'', resourceId:'', resourceType:'QUEUE'};
service.getAllChannels(filter).subscribe(data => { 
console.log(data); // see the console to make sure it is the mock
actualData = data; // assign actualData to the mock data
expect(actualData).toEqual(channels); // do your assertion here
done(); // call done to let Jasmine know we are done with our assertions.
});
backend.expectOne((req: HttpRequest<any>) => {
return req.url === `/MonitoraggioWS/rs/public/getListaCanaliWSR` && req.method === 'POST'
}, 'Load all channels from /MonitoraggioWS/rs/public/getListaCanaliWSR').flush(channels);
// expect(actualData).toEqual(channels); // doing the assertion here is too early
});

相关内容

最新更新