用typescript编写服务文件的测试用例
用typescript编写服务文件的测试用例,typescript,testing,jestjs,Typescript,Testing,Jestjs,我对编写测试用例比较陌生。我有一个typescript服务,我计划为它编写测试文件。它包含嵌套方法。它们不包含返回语句。我是否应该只为main方法编写测试用例。由于它不包含no return语句,如何测试我的文件?如果有人能帮我测试这个文件,我将不胜感激 @Injectable() export class MessageService { constructor( private readonly httpService: HttpService, private read
我对编写测试用例比较陌生。
我有一个typescript服务,我计划为它编写测试文件。它包含嵌套方法
。它们不包含返回语句。我是否应该只为main方法编写测试用例。由于它不包含no return
语句,如何测试我的文件?如果有人能帮我测试这个文件,我将不胜感激
@Injectable()
export class MessageService {
constructor(
private readonly httpService: HttpService,
private readonly messagePublishService: MessagePublishService,
private readonly configService: ConfigService,
) {}
message = new ProductMessage();
publishMessages(productEvent: ProductEvent): Observable<any> {
let json = '';
const url = this.configService.get('SITEMAP_ENDPOINT');
const urls = [url + '/sitemap/retrieve/ide', url + '/sitemap/retrieve/tpd'];
try {
for (let i = 0; i < urls.length; i++) {
return https.get(urls[i], res => {
res.on('data', chunk => {
json += chunk;
});
res.on('end', () => {
const result = JSON.parse(json);
getProductCodes(productEvent, result, this.messagePublishService);
});
});
}
} catch (err) {
console.error(err);
}
}
}
function getProductCodes(
productEvent,
result,
messagePublishService: MessagePublishService,
) {
const splitCodes = productEvent.productCodes.split(',');
const splitvehicleType = productEvent.vehicleType.split(',');
const source: string = 'PCS_DATACACHE_TOPIC_ARN';
for (let i = 0; i < result.length; i++) {
for (let j = 0; j < result[i].products.length; j++) {
if (
productEvent.productCodes.length !== 0 &&
productEvent.productCodes !== undefined
) {
searchProductCode(
messagePublishService,
splitCodes,
result,
i,
j,
source,
);
} else {
console.log('No data found');
return;
}
}
}
}
function searchProductCode(
messagePublishService,
splitCodes,
result,
i,
j,
source,
) {
for (let k = 0; k < splitCodes.length; k++) {
if (result[i].products[j].productCode === splitCodes[k]) {
console.log(
'Product Codes: ' +
result[i].products[j].productCode +
' Country : ' +
result[i].country +
' Language : ' +
result[i].language,
);
const message: string =
'Product Codes: ' +
result[i].products[j].productCode +
' Country : ' +
result[i].country +
' Language : ' +
result[i].language;
messagePublishService.publish(source, message);
}
}
}
@Injectable()
导出类消息服务{
建造师(
专用只读httpService:httpService,
私有只读messagePublishService:messagePublishService,
专用只读configService:configService,
) {}
message=newproductmessage();
publishMessages(productEvent:productEvent):可观察{
让json='';
const url=this.configService.get('SITEMAP_ENDPOINT');
常量url=[url+'/sitemap/retrieve/ide',url+'/sitemap/retrieve/tpd'];
试一试{
for(设i=0;i{
res.on('data',chunk=>{
json+=chunk;
});
res.on('结束',()=>{
const result=JSON.parse(JSON);
getProductCodes(productEvent、result、this.messagePublishService);
});
});
}
}捕捉(错误){
控制台错误(err);
}
}
}
函数getProductCodes(
productEvent,
结果,,
messagePublishService:messagePublishService,
) {
const splitCodes=productEvent.productCodes.split(',');
const splitvehicleType=productEvent.vehicleType.split(',');
const source:string='PCS\u DATACACHE\u TOPIC\u ARN';
for(设i=0;i
对于此类案件,您可以使用间谍
使用spies,您可以检查是否调用了类的方法(次数、给出的参数等)
const spy = spyOn(messagePublishService, "publish");
service.searchProductCode(...arguments);
expect(spy).toHaveBeenCalledWith("expectedSource", "expectedMessage")