GithubHelp home page GithubHelp logo

jasmine-sinon's Introduction

Jasmine matchers for Sinon.JS

Build Status

jasmine-sinon provides a set of custom matchers for using the Sinon.JS spying, stubbing and mocking library with Jasmine BDD.

Instead of:

expect(mySinonSpy.calledWith('foo')).toBeTruthy();

you can say:

expect(mySinonSpy).toHaveBeenCalledWith('foo');

This is not only nicerer to look at in your purdy specs, but you get more descriptive failure output in your Jasmine spec runner.

Instead of:

Expected false to be truthy.

you get:

Expected spy "mySpy" to have been called with "foo".

Jasmine 1.x / 2.x compatibility

If you are using Jasmine 1.x, use the latest 0.3.x release. For Jasmine 2, use 0.4 or above.

Installation

Direct include

Just include lib/jasmine-sinon.js in your Jasmine test runner file. Don't forget to include sinon.js.

With jasmine-gem

Add it to jasmine.yml. Don't forget to include sinon.js.

Node.js / NPM

npm install jasmine-sinon --save-dev

Then, in your jasmine spec:

var sinon = require('sinon');
require('jasmine-sinon');

Using Bower

bower install jasmine-sinon --save-dev

Then, include components/jasmine-sinon/index.js in your test runner.

Sinon.JS matchers

In general, you should be able to translate a Sinon spy/stub/mock API method to a jasmine-sinon matcher by prepending toHaveBeen to the front of the method name. For example, the Sinon.JS spy method called becomes toHaveBeenCalled. There are one or two exceptions to this rule, so the full list of matchers is given below.

Sinon.JS property / method jasmine-sinon matcher
called toHaveBeenCalled()
calledOnce toHaveBeenCalledOnce()
calledTwice toHaveBeenCalledTwice()
calledThrice toHaveBeenCalledThrice()
calledBefore() toHaveBeenCalledBefore()
calledAfter() toHaveBeenCalledAfter()
calledOn() toHaveBeenCalledOn()
alwaysCalledOn() toHaveBeenAlwaysCalledOn()
calledWith() toHaveBeenCalledWith()
alwaysCalledWith() toHaveBeenAlwaysCalledWith()
calledWithExactly() toHaveBeenCalledWithExactly()
alwaysCalledWithExactly() toHaveBeenAlwaysCalledWithExactly()
calledWithMatch() toHaveBeenCalledWithMatch()
alwaysCalledWithMatch() toHaveBeenAlwaysCalledWithMatch()
calledWithNew toHaveBeenCalledWithNew() >=v0.4
neverCalledWith toHaveBeenNeverCalledWith() >=v0.4
neverCalledWithMatch() toHaveBeenNeverCalledWithMatch() >=v0.4
threw() toHaveThrown()
alwaysThrew() toHaveAlwaysThrown()
returned() toHaveReturned()
alwaysReturned() toHaveAlwaysReturned()

These matchers will work on spies, individual spy calls, stubs and mocks.

You can use Jasmine spies alongside your Sinon spies. jasmine-sinon will detect which you're using and use the appropriate matcher.

You can also use Jasmine's fuzzy matchers any() and objectContaining() in expectations, e.g.

expect(spy).toHaveBeenCalledWith(jasmine.any(Date));
expect(spy).toHaveBeenCalledWith(jasmine.objectContaining({name: 'froots'}))

Contributors

Thanks to:

  • @aelesbao for Exception matchers
  • @theinterned for, er, match matchers
  • @milichev for graceful spy matchers
  • @reinseth for Jasmine fuzzy matcher support
  • @stoodder for initial Jasmine 2.0 support work

jasmine-sinon's People

Contributors

froots avatar milichev avatar reinseth avatar theinterned avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.