Skip to content

elcuervo/smoking

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Smoking

Simple Mocks and Stubs for javascript.

Build Status

Smock

$ npm install smoking

Or add it to your script tag

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

Stubbing

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

Example

var Fruit = function(color) {
  this.color = color;
  this.healthy = 'yes';
};

Fruit.prototype = {
  cutInPieces: function() {
    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

Another

var uberChangedFruit = smoking(redFruit, {
  color: 'blue',
  cutInPieces: function() {
    return 7;
  }
});

uberChangedFruit.color;
// 'blue'
uberChangedFruit.cutInPieces();
// 7
uberChangedFruit.healthy;
// 'yes'

Mocking

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.

Name

It's a foca's idea :D.

About

Simple Mocking and Stubbing for javascript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published