Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 检查是否使用sinon spy调用了存根getter函数_Unit Testing_Sinon_Spy - Fatal编程技术网

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');
    }
  }