我用两个存根对功能进行了测试,实际上存根正在工作,并且如果我在函数的任何部分中console.log res.json或next,它将显示内部的间谍对象。无论如何,间谍没有被调用,当我做出断言时,它说“预期间谍至少被调用过一次却从未被调用过”。该程序有效,但是测试很疯狂。请帮帮我!
我有这样的功能:
export function createOrUpdateToken(req, res, next) {
const { code, redirect_uri, realm_id, quickbooksAuth } = req.body;
if (!code || !redirect_uri || !realm_id) {
const message = 'Authorization code, Redirect URI and Realm Id are required';
return next(new CustomError('Bad Request', message, 400));
}
return issueRefreshTokenBasedOnAuthorizationCode(
{
redirect_uri,
authorizationCode: code,
authorization: quickbooksAuth,
})
.then((response) => {
const { body: { refresh_token } } = response;
return securityModel.findOneAndUpdate(
{ realmId: realm_id },
{ refreshToken: refresh_token },
{ upsert: true },
(err) => {
if (err) return next(err);
return res.json('Authentication successfull');
});
})
.catch((err) => {
return next(err);
});
}
测试是这样的:
it('Should create or update token', () => {
req.body = {
code: '1234',
redirect_uri: 'www.test.com',
realm_id: '12345',
quickbooksAuth: 'dhajksdas.dsajdosaiudjsa.dsaojpdas'
};
sinon
.stub(intuit, 'issueRefreshTokenBasedOnAuthorizationCode')
.resolves({
body: {
refresh_token: 'hjdklasdashda.dsa.dasdsa.dasddasdas'
}
});
sinon
.stub(securityModel, 'findOneAndUpdate')
.withArgs({ realmId: req.body.realm_id },
{ refreshToken: 'hjdklasdashda.dsa.dasdsa.dasddasdas' })
.yields(null);
createOrUpdateToken(req, res, next);
sinon.assert.called(res.json);
});
条件是:
beforeEach(() => {
res = {
json: sinon.spy()
};
next = sinon.spy();
});
afterEach(() => {
sinon.restore();
})
答案 0 :(得分:1)
createOrUpdateToken
执行一些异步代码,这些代码在返回之前尚未完成,sinon.assert.called(res.json)
运行失败。
您已经从createOrUpdateToken
返回了一个Promise,因此只需在运行该断言之前将您的测试作为async
测试和await
该Promise:
it('Should create or update token', async () => { // make the test function async
req.body = {
code: '1234',
redirect_uri: 'www.test.com',
realm_id: '12345',
quickbooksAuth: 'dhajksdas.dsajdosaiudjsa.dsaojpdas'
};
sinon
.stub(intuit, 'issueRefreshTokenBasedOnAuthorizationCode')
.resolves({
body: {
refresh_token: 'hjdklasdashda.dsa.dasdsa.dasddasdas'
}
});
sinon
.stub(securityModel, 'findOneAndUpdate')
.withArgs({ realmId: req.body.realm_id },
{ refreshToken: 'hjdklasdashda.dsa.dasdsa.dasddasdas' })
.yields(null);
await createOrUpdateToken(req, res, next); // await the returned Promise
sinon.assert.called(res.json); // SUCCESS
});