Reputation: 2871
I am trying to create a unit test to test that an object that my code has created is successfully sent to an update function (not testing the update function currently as that would be an integration test).
What I am trying to do is use a sinon mock to check that the input to the update method is correct:
var objectToUpdate = {
"_id": 55f019a32f55b4508b05a155, //mongodb _id assigned earlier in the code
etc...
}
var mock = sinon.mock(MyClass.prototype);
mock.expects("update").once().withArgs([objectToUpdate]);
otherClass.functionThatCallsUpdate(function(error, result){
(typeof error).should.equal.null;
mock.verify();
mock.restore();
done();
});
The problem is that the code I am testing is a part of the database population code, so I do not know what the _id of the object will be before the test is run as it is created earlier in the process, so the withArgs()
part of the test is failing.
Is it possible to specify a partial object for withArgs()
in a sinon mock, or is there another approach I should use?
A less useful (but better than nothing) option would be to be able to specify an expectation for the size of the array containing objectToUpdate
, but I can't find a way to do that either.
Upvotes: 0
Views: 933
Reputation: 2871
It is possible to make this work by removing the _id from the object and using sinon.match:
mock.expects("update").once().withArgs([sinon.match(objectToUpdate)]).yields(null);
Upvotes: 1