sinonStub.called prints false, though I am calling the function

无人久伴 提交于 2019-12-13 20:58:59

问题


I am running test on node js app using sinon. I want the sinonStub.called to be true but this prints false. I am calling the function indirectly( function call within function). I have given code snippet below

spec.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
    })    

Actual.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;
}


来源:https://stackoverflow.com/questions/56019297/sinonstub-called-prints-false-though-i-am-calling-the-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!