Skip to content

Commit

Permalink
Merge branch 'sinon-chai'. Fixes #14.
Browse files Browse the repository at this point in the history
  • Loading branch information
Misha Berezin committed Jun 14, 2013
2 parents 6daecf5 + 31521a5 commit dd2ad65
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 18 deletions.
3 changes: 2 additions & 1 deletion docs/tests.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@

Для тестирования используется фреймворк [mocha](http://visionmedia.github.io/mocha/) в режиме `bdd`.

Для ассертов используется библиотека [chai](http://chaijs.com/). Можно считать, что на момент запуска тестов метод chai.should() уже выполнен (http://chaijs.com/guide/styles/).
Для ассертов используется библиотека [chai](http://chaijs.com/) с плагином [sinon-chai](https://github.com/domenic/sinon-chai).
На момент запуска тестов метод `chai.should` уже выполнен (http://chaijs.com/guide/styles/).

Для моков используется библиотека [sinon](http://sinonjs.org/).

Expand Down
5 changes: 5 additions & 0 deletions test.blocks/sinon-chai/sinon-chai.deps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
({
mustDeps: [
{ block: 'chai' }
]
})
101 changes: 101 additions & 0 deletions test.blocks/sinon-chai/sinon-chai.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
modules.define('sinon-chai', ['chai'], function(provide, chai) {

(function (sinonChai) {
"use strict";

provide(sinonChai);

}(function sinonChai(chai, utils) {
"use strict";

var slice = Array.prototype.slice;

function isSpy(putativeSpy) {
return typeof putativeSpy === "function" &&
typeof putativeSpy.getCall === "function" &&
typeof putativeSpy.calledWithExactly === "function";
}

function isCall(putativeCall) {
return putativeCall && isSpy(putativeCall.proxy);
}

function assertCanWorkWith(assertion) {
if (!isSpy(assertion._obj) && !isCall(assertion._obj)) {
throw new TypeError(utils.inspect(assertion._obj) + " is not a spy or a call to a spy!");
}
}

function getMessages(spy, action, nonNegatedSuffix, always, args) {
var verbPhrase = always ? "always have " : "have ";
nonNegatedSuffix = nonNegatedSuffix || "";
if (isSpy(spy.proxy)) {
spy = spy.proxy;
}

function printfArray(array) {
return spy.printf.apply(spy, array);
}

return {
affirmative: printfArray(["expected %n to " + verbPhrase + action + nonNegatedSuffix].concat(args)),
negative: printfArray(["expected %n to not " + verbPhrase + action].concat(args))
};
}

function sinonProperty(name, action, nonNegatedSuffix) {
utils.addProperty(chai.Assertion.prototype, name, function () {
assertCanWorkWith(this);

var messages = getMessages(this._obj, action, nonNegatedSuffix, false);
this.assert(this._obj[name], messages.affirmative, messages.negative);
});
}

function createSinonMethodHandler(sinonName, action, nonNegatedSuffix) {
return function () {
assertCanWorkWith(this);

var alwaysSinonMethod = "always" + sinonName[0].toUpperCase() + sinonName.substring(1);
var shouldBeAlways = utils.flag(this, "always") && typeof this._obj[alwaysSinonMethod] === "function";
var sinonMethod = shouldBeAlways ? alwaysSinonMethod : sinonName;

var messages = getMessages(this._obj, action, nonNegatedSuffix, shouldBeAlways, slice.call(arguments));
this.assert(this._obj[sinonMethod].apply(this._obj, arguments), messages.affirmative, messages.negative);
};
}

function sinonMethodAsProperty(name, action, nonNegatedSuffix) {
var handler = createSinonMethodHandler(name, action, nonNegatedSuffix);
utils.addProperty(chai.Assertion.prototype, name, handler);
}

function exceptionalSinonMethod(chaiName, sinonName, action, nonNegatedSuffix) {
var handler = createSinonMethodHandler(sinonName, action, nonNegatedSuffix);
utils.addMethod(chai.Assertion.prototype, chaiName, handler);
}

function sinonMethod(name, action, nonNegatedSuffix) {
exceptionalSinonMethod(name, name, action, nonNegatedSuffix);
}

utils.addProperty(chai.Assertion.prototype, "always", function () {
utils.flag(this, "always", true);
});

sinonProperty("called", "been called", " at least once, but it was never called");
sinonProperty("calledOnce", "been called exactly once", ", but it was called %c%C");
sinonProperty("calledTwice", "been called exactly twice", ", but it was called %c%C");
sinonProperty("calledThrice", "been called exactly thrice", ", but it was called %c%C");
sinonMethodAsProperty("calledWithNew", "been called with new");
sinonMethod("calledBefore", "been called before %1");
sinonMethod("calledAfter", "been called after %1");
sinonMethod("calledOn", "been called with %1 as this", ", but it was called with %t instead");
sinonMethod("calledWith", "been called with arguments %*", "%C");
sinonMethod("calledWithExactly", "been called with exact arguments %*", "%C");
sinonMethod("calledWithMatch", "been called with arguments matching %*", "%C");
sinonMethod("returned", "returned %1");
exceptionalSinonMethod("thrown", "threw", "thrown %1");
}));

});
5 changes: 3 additions & 2 deletions test.blocks/test/test.deps.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
({
shouldDeps: [
mustDeps: [
{ block: 'i-ecma', elems: ['array', 'object'] },
{ block: 'mocha' },
{ block: 'chai' },
{ block: 'sinon' }
{ block: 'sinon' },
{ block: 'sinon-chai' }
]
})
30 changes: 15 additions & 15 deletions test.blocks/test/test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
(function(w) {
modules.require(['mocha', 'chai', 'sinon', 'sinon-chai'], function(mocha, chai, sinon, sinonChai) {

if(w.addEventListener) {
w.addEventListener('load', runTests, false);
}
else if(w.attachEvent) {
w.attachEvent('onload', runTests);
}
mocha.ui('bdd');

function runTests() {
modules.require(['mocha', 'chai', 'sinon'], function(mocha, chai, sinon) {
chai.use(sinonChai);
chai.should();

mocha.ui('bdd');
chai.should();
});

modules.require(['test'], function() {
w.mochaPhantomJS ? mochaPhantomJS.run() : mocha.run();
});
(function() {
onload(function() {
modules.require(['mocha', 'test'], function(mocha) {
window.mochaPhantomJS ? window.mochaPhantomJS.run() : mocha.run();
});
});

function onload(fn) {
if(window.addEventListener) { window.addEventListener('load', fn, false) }
else if(window.attachEvent) { window.attachEvent('onload', fn) }
}
})(window);
})();

0 comments on commit dd2ad65

Please sign in to comment.