尽管我正在调用该函数,但sinonStub.call打印为false

时间:2019-05-07 09:15:19

标签: node.js sinon-chai

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

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


0 个答案:

没有答案