问题
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