Unit testing 检查是否使用sinon spy调用了存根getter函数
我使用的是Unit testing 检查是否使用sinon spy调用了存根getter函数,unit-testing,sinon,spy,Unit Testing,Sinon,Spy,我使用的是firebase admin,我正试图为我的代码编写一些单元测试 由于admin被注入到我的函数中,我想我可以模拟这样一个非常简单的对象: admin = { get auth () { return { updateUser: () => { return true; }, createUser: () => { return true; }, getUser: ()
firebase admin
,我正试图为我的代码编写一些单元测试
由于admin
被注入到我的函数中,我想我可以模拟这样一个非常简单的对象:
admin = {
get auth () {
return {
updateUser: () => {
return true;
},
createUser: () => {
return true;
},
getUser: () => {
throw Error('no user');
}
};
}
};
然后在一个特定的测试中,我可以存根函数。以下是我迄今为止所做的工作:
// stubbed functions
sinon.stub(admin, 'auth').get(() => () => ({
updateUser: () => ({ called: true }),
getUser: () => (userRecord),
createUser: () => ({ called: false })
}));
这些都工作得很好(我可以通过我的日志看到)
然而,在我的测试中,我还想检查是否调用了createUser
。
我原以为我可以在createUser
函数上设置一个间谍,但到目前为止我还不能真正让它工作
以下是我一直在尝试的(有很多变化总是失败):
我试图测试的代码位(即upsert函数
是:
// in my test exisiting user is not null (the stub `getUser` is returning a object
if (existingUser != null) {
try {
await admin.auth().updateUser(uid, userDataForAuth);
return userDataForAuth;
} catch (error) {
console.log('error', error);
throw Error('error updating user');
}
}
我甚至不确定这是否是最好的方法,如果有更好的方法,我很乐意更改它!请提供测试中的代码。@slideshowp2我添加了与测试相关的位…希望对您有意义
// in my test exisiting user is not null (the stub `getUser` is returning a object
if (existingUser != null) {
try {
await admin.auth().updateUser(uid, userDataForAuth);
return userDataForAuth;
} catch (error) {
console.log('error', error);
throw Error('error updating user');
}
}