I'm having trouble tracking down why a sinon spy isn't being triggered. In the test below both console statements report as false so neither method is being called (in case there was an error).
This is what one of my mocha tests generally looks like:
describe('Post Controller', function () {
var controller = require('../controllers/PostController'),
req = {
user: {
check_id: "00100",
access_id: "54876329"
}
};
beforeEach(function () {
res = {
send: sinon.spy(),
json: sinon.spy()
};
});
afterEach(function () {
res.send.reset();
});
describe('readAllPosts', function () {
it('should return an array of posts', function (done) {
controller.readAllPosts(req, res);
process.nextTick(function () {
console.log('send: ', res.send.called);
console.log('json: ', res.json.called);
assert(res.json.calledWith(sinon.match.array));
done();
});
});
});
});
The readAllPosts method in PostContoller:
var postController = {
readAllPosts: function (req, res) {
PostModel.find(
{
access_id: req.user.access_id
},
function (err, results) {
if (err) {
res.send(404, err);
} else {
// res here is a spy
res.json(results);
}
}
);
}
};
And finally the find method in PostModel:
Posts.find= function (conditions, cb) {
/* ... */
dbQuery(query, function (err, results) {
if (err) {
cb(err);
}
cb(null, results);
});
};
If I call the methods in normal fashion they execute find, returning the expected array of Posts. However the res spy is never executed. Also, if I change the method in the controller class to this
var postController = {
readAllPosts: function (req, res) {
res.json([{this:"that",and:"other"}]);
}
};
the spied function (res) does fire. I know that the callback sent to the Model is being called and that the res object is there but it's not called. I don't think this is an indirection issue (calling the original method rather than the sinon wrapped one) but I'm not sure where the issue lies.