javascript - 单元测试具有回调函数的函数(请求模块)
问题描述
我正在我的 NodeJS 项目中进行测试,并且我想要对以下功能进行单元测试:
function myRequest(targetUrl, reqBody) {
return new Promise((resolve, reject) => {
request.post(targetUrl, { json: reqBody }, (error, response, body) => {
if (!error && response.statusCode === 200) {
resolve(body.transferId);
} else {
reject(error || body.description || body);
}
});
});
}
我正在使用摩卡和西农。如何测试此功能?
首先,我使用自己的模拟请求模块对成功场景进行了测试。现在,我想做错误场景,可能是 post 函数出错。我怎么能在不更改或制作新的请求模拟(返回错误)的情况下做到这一点?有可能吗?
解决方案
这是单元测试解决方案,您应该使用sinon.stub
:
例如
index.ts
:
import request from 'request';
export function myRequest(targetUrl, reqBody) {
return new Promise((resolve, reject) => {
request.post(targetUrl, { json: reqBody }, (error, response, body) => {
if (!error && response.statusCode === 200) {
resolve(body.transferId);
} else {
reject(error || body.description || body);
}
});
});
}
index.spec.ts
:
import { myRequest } from '.';
import chai from 'chai';
import sinon from 'sinon';
import chaiAsPromised from 'chai-as-promised';
import request from 'request';
chai.use(chaiAsPromised);
const { expect } = chai;
describe('myRequest', () => {
it('should request success', async done => {
// @ts-ignore
const stub = sinon.stub(request, 'post').callsFake((uri, options, callback) => {
const mResponse = { statusCode: 200 };
const mBody = { transferId: 1 };
callback(null, mResponse, mBody);
done();
});
const actualValue = await myRequest('url', {});
// @ts-ignore
stub.calledOnceWith('url', { json: {} });
expect(actualValue).to.eq(1);
stub.restore();
});
it('should throw error use request error', async done => {
const mError = new Error('Internal server error');
const mResponse = { statusCode: 500 };
// @ts-ignore
const stub = sinon.stub(request, 'post').callsFake((uri, options, callback) => {
callback(mError, mResponse, null);
done();
});
await expect(myRequest('url', {})).to.be.rejectedWith(mError);
// @ts-ignore
stub.calledOnceWith('url', { json: {} });
stub.restore();
});
it('should throw error use body.description as error message', async done => {
const mResponse = { statusCode: 500 };
const mBody = { description: 'some error' };
// @ts-ignore
const stub = sinon.stub(request, 'post').callsFake((uri, options, callback) => {
callback(null, mResponse, mBody);
done();
});
await expect(myRequest('url', {})).to.be.rejectedWith(mBody.description);
// @ts-ignore
stub.calledOnceWith('url', { json: {} });
stub.restore();
});
it('should throw error use body as error message', async done => {
const mResponse = { statusCode: 500 };
const mBody = 'some error';
// @ts-ignore
const stub = sinon.stub(request, 'post').callsFake((uri, options, callback) => {
callback(null, mResponse, mBody);
done();
});
await expect(myRequest('url', {})).to.be.rejectedWith(mBody);
// @ts-ignore
stub.calledOnceWith('url', { json: {} });
stub.restore();
});
});
覆盖率 100% 的单元测试结果:
myRequest
✓ should request success
✓ should throw error use request error
✓ should throw error use body.description as error message
✓ should throw error use body as error message
4 passing (13ms)
---------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.spec.ts | 100 | 100 | 100 | 100 | |
index.ts | 100 | 100 | 100 | 100 | |
---------------|----------|----------|----------|----------|-------------------|
源代码:https ://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/58822996
推荐阅读
- android - Android Webkit CookieManager:从 SQLite3 数据库加载 Cookie
- python - 我正在尝试在我的 Django 出勤应用程序中保存出勤率,但不断收到 TypeError、预期的字符串或类似字节的对象
- sql - 在一个或两个查询中计算来自两列的数据
- python - 如何让我的新 pyhon 类的回归正常工作?
- c++ - 如何将 T 类型的可变参数模板重写为特定(非原始)类型
- windows - 如何执行 Ansible AWX 模板?
- wordpress - 在 Plesk 中迁移后的 Wordpress 404 问题(Nginx 配置问题)
- python - PyQt将应用程序外部拖放到Windows文件资源管理器中?
- javascript - Console.log 将我的元素显示为数组而不是元素
- node.js - nodejs dgram udp socket监听特定接口