Mocks vs Stubs
Types of tests
- unit test
- integrated test
- functional test
Mock objects are used to define expectations i.e: In this scenario I expect method A() to be called with such and such parameters. Mocks record and verify such expectations.
Title Text
it('calls the original function', function () {
var callback = sinon.spy();
var proxy = once(callback);
proxy();
assert(callback.called);
});
Stubs, on the other hand have a different purpose: they do not record or verify expectations, but rather allow us to “replace” the behavior, state of the “fake”object in order to utilize a test scenario
var handler = function(callback){
//stupid handler that makes every file urgent
fs.readFile("/this/path/does/not/exist/", function(err, data) {
if (err) {
console.log(err)
}
callback(data+"!!!!");
})
}
module.exports = handler
var fs = require('fs');
var sinon = require('sinon');
sinon.stub(fs, 'readFile').yields(null, "LOLYESITDOES")
var test = require('tape');
var handler = require('./script.js')
test ('test the urgent handler', function(t){
handler(function(data){
t.equal(data, "LOLYESITDOES!!!!")
t.end()
})
});
When to write a test
- Classic
- TDD / BDD
Mocks vs Stubs
By Vladimir
Mocks vs Stubs
- 132