Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Mocks. Stubs. Spies. Node.js
JavaScript
branch: master

This branch is 45 commits behind jackjs:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
support
test
.gitignore
.travis.yml
History.md
Makefile
Readme.md
index.js
jack.js
package.json

Readme.md

Build Status

Jack

Description

Jack is a simple but yet powerful mock framework, that can be used with any testing framework with your Node.js module, or in the browser.

Synopsis

Mocks, Stubs, Spies.

var jack = require('jack');

obj.stub('foo');
obj.mock('foo');
obj.spy('foo');

jack.stub(obj, 'foo');
jack.mock(obj, 'foo');
jack.spy(obj, 'foo');

Stub

return

obj.stub('on').and.return(3);
obj.stub('foo').and.return(3, 4, 5);

replace

obj.stub('on').and.replace(function() {
  // do magic here.
});

reset

obj.foo.reset(); // in order to reset the method

throw

obj.stub('foo').and.throw('Message'); 

Mock

calls

obj.mock('on').it.should_be.called.once;

obj.mock('on').it.should_be.called.twice;

obj.mock('on').it.should_be.called.exactly(3).times;

obj.mock('on').it.should_be.called.any.number.of.times;

obj.mock('on').it.should_be.called.at.least(1).times;

arguments

obj.mock('on').expect.arguments('foo', 'bar');

You should call reset to check if an expectation is met.

obj.on.reset();

Spy

Spies are the same as mocks, but the original behavior of the method is kept.

obj.spy('foo');

No conflict mode

var jack = require('jack').noConflict();

Extend your object/class

var jack = require('jack');
var myObj = jack.extend(myObj);

function Obj() {}

jack.extend(Obj);

Requirements

Install

$ npm install jack

Chai Integration

Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.

For more information view the documentation.

Getting Started

Include chai and jack in your testing environment.

var chai = require('chai')
  , jack = require('jack');

chai.use(jack.chai);

var expect = chai.expect;

Usage

The jack integration with Chai provides a unique approach to mocks/stubs. Assertions can be defined before or after the test has taken place. This feature is referred to as tense detection and is an experimental feature provided by Chai, and works in the same way as negation. Where the keyword not will negate a statement, the keyword been will indicate that we are asserting how something has already behaved. This feature is only available in the jack/chai integration. Here is an example:

Given our Foo object that has a method bar:

var myfoo = new Foo();
myfoo.stub('bar');

If we wanted to assert how we expect this stub to behave in the future, we can create a new future tense assertion:

expect(myfoo).to.be.called.once;

With this, when we reset our stub with myfoo.bar.reset(), if that assertion is invalid, the reset will throw. We can attach as many assertions to the stub as needed, but be aware of conflicts.

If, instead, we prefer to test it after we have finished our work, we can create a past tense assertion:

expect(myfoo).to.have.been.called.once;

In this example, we are asserting after all of the work has been done but before the reset. Chai will look into the internals of the stub and determine if an assertion has passed or not. This behavior is a more native chai approach.

API Guide

.stub

Assert the object is a Jack stub.

expect(myfoo.bar).to.be.stub;

.called

Language chain. Negation passes through.

// past
expect(myfoo.bar).to.have.been.called;
// future
expect(myfoo.bar).to.be.called; // call count > 0

.not_called

Asserts that a stub has not been called. Essentially, negation for called.

expect(myfoo.bar).to.have.been.not_called;

.once

Asserts that stub has been called once.

// past
expect(myfoo.bar).to.have.been.called.once;
// future
expect(myfoo.bar).to.be.called.once;

.twice

Asserts that stub has been called twice.

// past
expect(myfoo.bar).to.have.been.called.twice;
// future
expect(myfoo.bar).to.be.called.twice;

.exactly(n)

Asserts that stub has been called exactly n times.

// past
expect(myfoo.bar).to.have.been.called.exactly(3);
// future
expect(myfoo.bar).to.be.called.exactly(3);

.min(n)

Asserts that stub has been called a minimum of n times.

// past
expect(myfoo.bar).to.have.been.called.min(3);
// future
expect(myfoo.bar).to.be.called.min(3);

.max(n)

Asserts that stub has been called a maximum of n times.

// past
expect(myfoo.bar).to.have.been.called.max(3);
// future
expect(myfoo.bar).to.be.called.max(3);

Roadmap

Chai integration will be expanding its assertions to include calledWith and other argument assertions in the near future.

Tests

$ npm install
$ make test

Where to Get Help

Please post issues to GitHub Issues.

Tests

Tests are written in the BDD styles for the Mocha test runner using the multiple assertion interfaces from Chai. Running tests is simple:

make test

A browser suite is also available at test/browser/index.js. The same test definitions are used in both contexts.

Contributing

Interested in contributing? Fork to get started.

Contibutors

License

MIT License

Copyright (C) 2012 Veselin Todorov and Jake Luer.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Something went wrong with that request. Please try again.