如何在以下函数上实现sinon.mock。
function getDashboard(req,res){res.send(“success”); }
describe("GetDashboard test"){
it("Response Should be test", function(){
const getDashboard = sinon.stub().returns('success');
let req = {}
let res = {
send: function(){};
const mock = sinon.mock(res);
mock.expect(getDashboard.calledOnce).to.be.true;
mock.verify();
}
})
}
还有如何在函数中存根数据。这是正确的模拟方式。
这是一个工作示例:
const sinon = require('sinon');
function getDashboard(req, res) { res.send('success'); }
describe("getDashboard", function () {
it("should respond with 'success'", function () {
const req = {};
const res = { send: sinon.stub() };
getDashboard(req, res);
sinon.assert.calledWithExactly(res.send, 'success'); // Success!
})
});
细节
getDashboard
调用它给出的send
对象的res
函数,因此你只需要为sinon
属性创建一个带有send
存根的模拟对象,并验证它是否按预期调用。