javascript - 在 nodejs 中拦截外部 API 调用
问题描述
js),我想使用 mocha 和 chai 正常测试它,但是 API 依赖于我想模拟的外部服务。是否可以模拟以下内部调用,例如以清晰显示:
describe('account Link', function() {
it('should return with account status', async function(done) {
nock('http://api.github.com', { allowUnmocked: true })
.get('/users/AKGP')
.reply(200,
{
test: true
});
const options = {
uri: 'http://localhost:3000/internal_api',
body: {},
headers: {
'content-type': 'application/json'
},
method: 'POST',
json: true
}
try {
const res = await rp(options)
console.log(res)
done();
} catch(err){
console.log(err);
done();
}
});
});
// the nocked api is called internally and I am expecting to mock it (http://api.github.com)
解决方案
推荐阅读
- node.js - how to host angular and node js project on the shared hosting (cpanel or plesk)?
- awesome-wm - 当 input_passthrough 为真时,awesome-wm wibox 对鼠标信号没有反应
- c++ - 无法使用 CMake 编译 MagickWand
- spring-data-jpa - SimpleJdbcCall 跳过第一个输入参数
- c - 将引号中的字符串传输到 C 中的 ascii2 字节数组
- sql - 是否有任何查询我们必须只写我们不想显示的列?SQL / Laravel
- python - 熊猫按年分组,日期产生虚假值
- r - Filter_at selected columns with multiple str_detect patterns
- javascript - 更新状态使数组呈现错误
- c++ - 我的重载运算符 < 不适用于 cpp 中的排序功能