Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/elcuervo/smoking
Simple Mocking and Stubbing for javascript
https://github.com/elcuervo/smoking
Last synced: 30 days ago
JSON representation
Simple Mocking and Stubbing for javascript
- Host: GitHub
- URL: https://github.com/elcuervo/smoking
- Owner: elcuervo
- Created: 2011-12-29T20:19:39.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2012-03-08T20:23:57.000Z (over 12 years ago)
- Last Synced: 2023-04-10T15:48:59.202Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 109 KB
- Stars: 10
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Smoking
Simple Mocks and Stubs for javascript.[![Build Status](https://secure.travis-ci.org/elcuervo/smoking.png?branch=master)](http://travis-ci.org/elcuervo/smoking)
![Smock](http://www.omerique.net/polavide/rec_polavide0708/edilim/hie_hue_hui_hum/misrecursos/humo.gif)
```bash
$ npm install smoking
```Or add it to your script tag
```html
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
```javascript
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();
// 5var stubbedRedFruit = smoking(redFruit, { healthy: 'a bit' });
stubbedRedFruit.healthy;
// 'a bit'
stubbedRedFruit.color;
// 'red'
stubbedRedFruit.cutInPieces();
// 2
redFruit.healthy;
// 'yes'
// You get the point
```### Another
```javascript
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.
```javascript
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
```javascript
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](http://github.com/foca)'s idea :D.