mochito
v0.0.2
Published
A hygenic JsMockito wrapper for modern test frameworks like Mocha.
Downloads
30
Maintainers
Readme
Mochito
Mochito is a hygenic JsMockito / JsHamcrest wrapper for modern test frameworks like Mocha. It aims to make JsMockito more competitive when compared with Sinon.JS.
Rationale
Despite its popularity, Sinon.JS has a number of negatives, including:
- You can't assert on a spy or stub; you can only ask a question that returns a boolean, which provides no error info if it fails.
- but, ... you have to use a spy or stub if you need a function rather than an object.
- Mocks have to define all expectations up front (leading to overly tight specifications), rather than allowing assertions to be made post-hoc.
- As a consequence, you can't do Given-When-Then style testing with Sinon.JS because what would otherwise be the then clause must appear before the when clause.
- Mocked methods are only given the ability to verify expectations after some expectations have been added, often preventing the use of the mock when it's first created.
- The
mock()
function returns a configurator object rather than the mock itself, which is somewhat confusing. - You need to remember to verify your expectations in your tear down methods or tests may pass when they shouldn't.
- Its deep equality checking method doesn't consider an array to be the same as an argument array containing the same items.
Unfortunately, JsMockito has some downsides too (which Mochito fixes):
- It uses lots of global variables rather than allowing you to require a single item.
- It's difficult to integrate into your testing framework — as a consequence of using globals.
- It requires you to separately include JsHamcrest, but doesn't work if you include a version that isn't exactly the version it's using.
- The error messages don't read very well because JsMockito throws strings
which are subsequently converted to
Error
instances by the containing test framework (will be fixed by this issue).
Usage
Using mochito is simple, for example:
var mochito = require('mochito');
describe('mochito', function() {
it('allows mocking to be performed with no set-up required', function() {
var mock = mochito.mock({
doSomething: function() {}
});
mock.doSomething('x', 'y');
mock.doSomething('z', 'y');
mochito.verify(mock, mochito.once()).doSomething('x', 'y');
mochito.verify(mock, mochito.once()).doSomething('z', 'y');
mochito.verify(mock, mochito.times(2)).doSomething(mochito.anything(), 'y');
});
});
Global Functions
If you really want the JsHamcrest & JsMockito functions to be available globally, you can simply write:
mochito.installTo(global);