【发布时间】:2019-06-18 14:28:31
【问题描述】:
我正在测试一个简单的快速路由,它返回一个 json 对象并使用 mocha 和 sinon 来匹配响应。
这是简单的快速响应 /login
const login = async (req, res) => {
let response = {
success: true,
id: "id0090",
}
res.end(JSON.stringify(response));
}
这是 micha 和 sinon 的简单春节
it('res.end should be called with success true ', async () => {
let req = {
body: {}
}
let res = {
end: sinon.spy(),
}
await login(req,res);
let expected = {
success: true,
id: "id0090",
}
sinon.assert.calledWith(res.end, sinon.match(expected));
});
我收到此错误,但正如您所见,两者是相同的。
AssertError: expected end to be called with arguments
{"success":true,"id":"id0090"} match(success: true, id: id0090)
有趣的是,如果我从 login 中删除 JSON.stringify,则测试通过,但这不是想法。
const login = async (req, res) => {
let response = {
success: true,
id: "id0090",
}
res.end(response); // this make the test pass
}
感谢您的帮助
【问题讨论】:
标签: javascript unit-testing mocha.js sinon