I’ve created a database wrapper for my application, shown below. To test it, I obviously would like to replace the actual database library. I could create a new class that mocks the query method and catch all input there, but using sinon.js seems more appropriate, but how would I use it?
Is the mock or stub features of sinon.js what I should be using?
wrapper = (function() {
function wrapper() {}
wrapper.db = require("database");
wrapper.prototype.insertUser = function(doc) {
return this.db.query("INSERT INTO USERS...");
};
return wrapper;
})();
You can use both for that.
Mock have an expected ordered behavior that, if not followed correctly, is going to give you an error.
A Stub is a similar to a mock, but without the order, so you can call your methods the way you want.
In my experience you almost never need a mock.
Both of them will substitute your method for an empty method, or a closure if you pass one.
It would be something like this:
Then you add the expect behavior to check if it did happened.
I like to use Jasmine with Jasmine-Sinon for checking the tests.