我需要使用sinon测试来测试控制器方法 但是无法使用同一方法调用两个外部功能。
我已经尝试过了
describe('getContactDetails', () => {
let req, functionToTest = sinon.stub();
before(() => {
functionToTest = leadsController.__get__('getContactDetails');
});
beforeEach(() => {
req = {};
});
it('Should catch any error sent back from the model', async() => {
let res = getRes();
contactModelStub.storedProcCall.throws(new Error('Stubbed output'));
contactModelStub.getModelQuery.throws(new Error('Stubbed output'));
await functionToTest(req, res);
await expect(res.status.getCall(0).args[0]).to.equal(500);
await expect(res.send.getCall(0).args[0]).to.equal('There was an error with the model call.');
await expect(loggerStub.error.calledOnce).to.be.true;
});
it('Should return the response from the model', async() => {
let res = getRes(),
responseModel = [{
"id": 19107,
"text": "williyams"
}];
contactModelStub.getModelQuery.returns(modelResponse);
await functionToTest(req, res);
await expect(res.send.getCall(0).args[0]).to.deep.equal(responseModel);
});
});
这是控制者
async function getContactDetails(req, res) {
const session = req.tbSession;
try {
await contactModel.storedProcCall(session['myid'][20]);
req.query.clientId = session['clientId'];
let contactDetails = await contactModel.getModelQuery(req.query);
res.status(200).send(contactDetails).end();
} catch (error) {
logger.error(error);
res.status(500).send('error with database call').end();
}
}
如果我不使用第一个电话
await contactModel.storedProcCall(session['myid'][20]);
它工作正常
但是如果在之前被调用
await contactModel.getModelQuery(req.query);
它将错误抛出为数据库调用错误
任何人请帮助我。