unexpected-sinon

This module extends the [Unexpected](https://github.com/sunesimonsen/unexpected) assertion library with integration for the [Sinonjs](http://sinonjs.org/) mocking library.

npm install unexpected-sinon
19 downloads in the last week
42 downloads in the last month

Unexpected-sinon

This module extends the Unexpected assertion library with integration for the Sinonjs mocking library.

expect(spy, "was called twice");
expect(spy, 'was called with', { foo: 'bar' }, 'baz', sinon.match.truthy);
expect(obj.spy, 'was always called on', obj);

Build Status

Run the test in the browser

How to use

Node

Install it with NPM or add it to your package.json:

$ npm install unexpected unexpected-sinon

Then:

var expect = require('unexpected').clone();
expect.installPlugin(require('unexpected-sinon'));

Browser

Include the unexpected.js found at the lib directory of this repository.

<!-- SinonJS -->
<script src="sinon.js"></script>
<script src="sinon/spy.js"></script>
<script src="sinon/stub.js"></script>
<script src="sinon/mock.js"></script>
<script src="sinon/collection.js"></script>
<script src="sinon/assert.js"></script>
<script src="sinon/sandbox.js"></script>
<script src="sinon/test.js"></script>
<script src="sinon/test_case.js"></script>
<script src="sinon/assert.js"></script>
<script src="sinon/match.js"></script>
<!-- Unexpected -->
<script src="unexpected.js"></script>
<script src="unexpected-sinon.js"></script>

this will expose the expect function under the following namespace:

var expect = weknowhow.expect.clone();
expect.installPlugin(weknowhow.unexpectedSinon);

RequireJS

Include the library with RequireJS the following way:

define(['unexpected', 'unexpected-sinon'], funtion (unexpected, unexpectedSinon) {
   var expect = unexpected.clone();
   expect.installPlugin(unexpectedSinon);
   // Your code
});

API

was called

Passes if spy was called at least once.

expect(spy, 'was called');

was not called

Passes if spy was never called.

expect(spy, 'was not called');

was called once

Passes if spy was called once and only once.

expect(spy, 'was called once');

was called twice

Passes if spy was called exactly twice.

expect(spy, 'was called twice');

was called thrice

Passes if spy was called exactly three times.

expect(spy, 'was called thrice');

was called times

Passes if the spy was called exactly num times.

expect(spy, 'was called times', 42);

given call order

Passes if the provided spies where called in the specified order.

expect([spy0, spy1, spy2], 'given call order');

was called on

Passes if the spy was ever called with obj as its this value.

expect(obj.spy, 'was called on', obj);

was always called on

Passes if the spy was always called with obj as its this value.

expect(obj.spy, 'was always called on', obj);

was called with

Passes if the spy was called with the provided arguments.

expect(spy, 'was called with', { foo: 'bar' }, 'baz', sinon.match.truthy);

was always called with

Passes if the spy was always called with the provided arguments.

expect(spy, 'was always called with', { foo: 'bar' }, 'baz', sinon.match.truthy);

was never called with

Passes if the spy was never called with the provided arguments.

expect(spy, 'was never called with', 'bar', sinon.match.truthy);

was called with exactly

Passes if the spy was called with the provided arguments and no others.

expect(spy, 'was called with exactly', 'foo', 'bar', sinon.match.truthy);

was always called with exactly

Passes if the spy was always called with the provided arguments and no others.

expect(spy, 'was always called with exactly', 'foo', 'bar', sinon.match.truthy);

threw

Passes if the spy threw the given exception. The exception can be a string denoting its type, or an actual object. If only one argument is provided, the assertion passes if the spy ever threw any exception.

expect(spy, 'threw');
expect(spy, 'threw', 'TypeError');
expect(spy, 'threw', error);

always threw

Like above, only required for all calls to the spy.

expect(spy, 'always threw');
expect(spy, 'always threw', 'TypeError');
expect(spy, 'always threw', error);
npm loves you