node.js - 测试时拦截axios请求
问题描述
我正在测试一个软件,我想验证发送到 API 的请求是否具有正确的数据。这个特定的方法创建一个包含某些数据、标头等的请求,然后通过axios
. 示例代码:
myFunction() {
const data = {
example: 'my data'
};
const headers = {
'content-type': 'application/json'
}
const request = {
method: 'POST',
baseUrl: 'http://myawesome-site.com',
url: '/api/path',
headers,
data,
}
return axios(request)
.then(res => ...do something)
.catch(err => ...do something else)
}
我想知道是否有办法使用chai
或sinon
拦截 axios 调用并仅访问“请求”对象以验证正在发送的数据,我不关心响应。
解决方案
这是一个单元测试解决方案,您应该使用sinon.stub
:
当您想阻止直接调用特定方法时(可能是因为它触发了不希望的行为,例如 XMLHttpRequest 或类似的
例如
index.ts
:
import axios, { AxiosRequestConfig } from 'axios';
exports.myFunction = function myFunction() {
const data = {
example: 'my data'
};
const headers = {
'content-type': 'application/json'
};
const request: AxiosRequestConfig = {
method: 'POST',
baseURL: 'http://myawesome-site.com',
url: '/api/path',
headers,
data
};
return exports
.axios(request)
.then(res => console.log('do something'))
.catch(err => console.log('do something else'));
};
exports.axios = axios;
index.spec.ts
:
const mod = require('./');
import sinon from 'sinon';
import { expect } from 'chai';
describe('myFunction', () => {
afterEach(() => {
sinon.restore();
});
it('should send request', async () => {
const mResponse = { status: 200 };
const axiosStub = sinon.stub(mod, 'axios').resolves(mResponse);
const logSpy = sinon.spy(console, 'log');
await mod.myFunction();
expect(
axiosStub.calledWith({
method: 'POST',
baseURL: 'http://myawesome-site.com',
url: '/api/path',
headers: {
'content-type': 'application/json'
},
data: {
example: 'my data'
}
})
).to.be.true;
expect(logSpy.calledWith('do something')).to.be.true;
});
it('should handle request error', async () => {
const mError = new Error('network error');
const axiosStub = sinon.stub(mod, 'axios').rejects(mError);
const logSpy = sinon.spy(console, 'log');
await mod.myFunction();
expect(
axiosStub.calledWith({
method: 'POST',
baseURL: 'http://myawesome-site.com',
url: '/api/path',
headers: {
'content-type': 'application/json'
},
data: {
example: 'my data'
}
})
).to.be.true;
expect(logSpy.calledWith('do something else')).to.be.true;
});
});
覆盖率 100% 的单元测试结果:
myFunction
do something
✓ should send request
do something else
✓ should handle request error
2 passing (14ms)
---------------|----------|----------|----------|----------|-------------------|
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/57807855
推荐阅读
- java - CompletableFuture 正在阻塞线程
- c# - 当 System.CommandLine 已经收集了这些参数时,如何将命令行参数传递给 Cake (Frosting)?
- swift - UserDefault 不适用于 Swift 5 中的 M1 芯片 MacBook Air 和 Xcode 模拟器
- wso2 - 关于作为 wso2 apim 客户端的 spa 的安全性
- python - 从多个线程编辑数据框中的行
- c# - Asp.Net - Stripe 支付意图在本地工作,但在线出现错误 502
- javascript - 在 Webpack 中进行代码拆分后,Terser 插件会抛出 Invalid syntax
- flutter - Flutter中使用TextEditingController时如何前后移动光标?
- c# - C# 将特定日期时间格式化为字符串
- spring - 在 Tomcat 上进行 LDAP 身份验证后的 Spring Boot ServletException