smoking

Smoking: Simple Mocking and Stubbing for javascript

Smoking

Simple Mocks and Stubs for javascript.

$ npm install smoking

Or add it to your script tag

https://raw.github.com/elcuervo/smoking/master/lib/smoking.js

The idea is to provide simple mocking and stubbing to normal objects, no API, no dependencies.

var Fruit = function(color) {
  this.color = color;
  this.healthy = 'yes';
};
 
Fruit.prototype = {
  cutInPiecesfunction() {
    return Math.floor(Math.random()*11);
  }
};
 
var redFruit = new Fruit('red');
 
redFruit.color;
// 'red' 
redFruit.healthy;
// 'yes' 
redFruit.cutInPieces();
// 5 
 
var stubbedRedFruit = smoking(redFruit, { healthy: 'a bit' });
 
stubbedRedFruit.healthy;
// 'a bit' 
stubbedRedFruit.color;
// 'red' 
stubbedRedFruit.cutInPieces();
// 2 
redFruit.healthy;
// 'yes' 
// You get the point 
var uberChangedFruit = smoking(redFruit, {
  color: 'blue',
  cutInPiecesfunction() {
    return 7;
  }
});
 
uberChangedFruit.color;
// 'blue' 
uberChangedFruit.cutInPieces();
// 7 
uberChangedFruit.healthy;
// 'yes' 

You can easily verify the call of methods.

var mockFruit = smoking(redFruit).expects({ cutInPieces: 1 });
mockFruit.cutInPieces();
smoking(mockFruit).verify();

Or with a shorthand if it's just one method and needs to be called once

var mockFruit = smoking(redFruit).expects('cutInPieces');
smoking(mockFruit).verify();

The prior example will fail with a RangeError because the 'cutInPieces' methods does not get called.

It's a foca's idea :D.