首页 > 解决方案 > sinonStub.called 打印错误,尽管我正在调用该函数

问题描述

我正在使用 sinon 在节点 js 应用程序上运行测试。我希望 sinonStub.call 为真,但这打印为假。我间接调用函数(函数内的函数调用)。我在下面给出了代码片段

规范.js

describe.only('creating stub for Accounts method',function(){
   mockResponse=
        [
         {
         "AccountID": "xyz",
           ....
         }
         ]

        req1= {
            user:{
                id:""
            },
       },

       res1= {
       json:sinon.spy()//Is this correct
       }

     it('should call  getActivatedAccounts and always this mock response',function(){
        var getAccountsStub=sinon.stub(devices,'getAccounts').returns(mockResponse);
        devices.getActivatedResponse(req1,res1);

        console.log(getAccountsStub.called);//I expect this to be called
    })    

实际.js

function getActivatedResponse(req, res) {

    if (!req.user || !req.user.id) {
      let reply = {
        status : "SUCCESS",
        data : []
      }
      res.json(reply);
      //console.log(reply);
    } else {
      getActivatedAccounts(req.user.id).then(
        function(reply) { 
          res.json(reply);
        },
        function(error) {
          console.log(error);
        }
      );
    }
function getAccounts(Id){
....
....
returns promise;
}


标签: node.jssinon-chai

解决方案


推荐阅读