Unexpected-sinon

Unexpected spy :)

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

NPM version Build Status Coverage Status Dependency Status

Usage

Here is an example an example stolen from Gary Bernhardt.

function SucksRocks(searchEngine) {
  this.forTerm = function (term) {
    return Promise.all([
      searchEngine.countResults(term + " rocks"),
      searchEngine.countResults(term + " sucks")
    ]).then(function (results) {
      var positive = results[0];
      var negative = results[1];
      return positive > negative ? 'Rocks!' : 'Sucks!';
    });
  };
}
var searchEngine = {
  countResults: sinon.stub()
};
 
searchEngine.countResults
  .onFirstCall().returns(Promise.resolve(6920000))
  .onSecondCall().returns(Promise.resolve(3400000));
 
var result = new SucksRocks(searchEngine).forTerm('Open source');
 
expect(searchEngine.countResults, 'to have calls satisfying', function () {
  searchEngine.countResults('Open source rocks');
  searchEngine.countResults('Open source sucks');
});
 
return expect(result, 'when fulfilled', 'to equal', 'Rocks!');

Amazing output when a failure happens!

var searchEngine = {
  countResults: sinon.stub()
};
 
searchEngine.countResults
  .onFirstCall().returns(Promise.resolve(6920000))
  .onSecondCall().returns(Promise.resolve(3400000));
 
var score = new SucksRocks(searchEngine).forTerm('Open source');
 
expect(searchEngine.countResults, 'to have calls satisfying', function () {
  searchEngine.countResults('Open source rocks!');
  searchEngine.countResults('Open source sucks!');
});
expected stub to have calls satisfying
stub'Open source rocks!' );
stub'Open source sucks!' );
 
stub(
  
'Open source rocks' 
//
//
//
//
 
should equal 
'Open source rocks!'
 
Open source rocks
Open source rocks!
); at theFunction (theFileName:xx:yy)
stub(
  
'Open source sucks' 
//
//
//
//
 
should equal 
'Open source sucks!'
 
Open source sucks
Open source sucks!
); at theFunction (theFileName:xx:yy)

Setup

Node

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

$ npm install --save-dev unexpected unexpected-sinon

Then:

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

Browser

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

<script src="sinon.js"></script>
<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.use(weknowhow.unexpectedSinon);

RequireJS

Include the library with RequireJS the following way:

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

Because Sinon is currently not AMD compatible you will need this RequireJS configuration to make things work:

requirejs.config({
   paths: {
      'sinon': 'path/to/sinon'
   },
   shim: {
      'sinon': {
         deps: ['sinon'],
         exports: 'sinon'
      }
   }
});

Source

The source for Unexpected can be found on Github.

Release

6.0.0

MIT License

Copyright (c) 2013 Sune Simonsen sune@we-knowhow.dk

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.

Fork me on GitHub