【发布时间】:2021-11-26 18:37:36
【问题描述】:
如何使用 Mocha、sinon 和 chai 测试 NPM 请求库?
我得到一个Error: getaddrinfo ENOTFOUND。 URL 应该无关紧要,因为我希望 yields 无论 url 是什么值都会返回
describe(`api tests`, () => {
it(`should return`, async () => {
sinon.stub(request, `get`).yields(null, null, JSON.stringify({test: `teststub`}))
return apiFunction.then(res => {
assert.equal(res.body, {test: "stubtest"})
})
})
})
const apiFunction () => {
request(
{
url: `http://url`
},
(err, response, body) => {
console.log(body) // should be {test: "subtest"}
})
}
【问题讨论】:
标签: node.js mocking mocha.js sinon