-
Notifications
You must be signed in to change notification settings - Fork 0
Custom Tests
Jeremy Greer edited this page Sep 8, 2014
·
2 revisions
To define new tests, use willy.define
. The function should return an expression used to determine if the test passes or fails.
willy.define(function beASubstringOf() {
return this.expected.indexOf(this.actual) > -1;
});
will('potato').beASubstringOf('Bender Bending Rodriguez');
// expected 'potato' to be a substring of 'Bender Bending Rodriguez'
Inside every test function
-
this.actual
is the value passed towill()
-
this.expected
is the optional value passed to the test
Pass an object to willy.define
to give you more options. The object's properties should be:
- fn - The function used to perform the test.
- explanation (optional) - An explanation of what you were testing. If omitted, the test's name will be converted.
- name (optional) - The name of your test. This can usually be figured out, but ocassionally you may want to be explicit.
Changing the Explanation
// providing a better explanation
willy.define({
fn: function beASubstringOf() {
return this.expected.indexOf(this.actual) > -1;
},
explanation: 'be found inside of'
});
will('potato').beASubstringOf('Bender Bending Rodriguez');
// expected 'potato' to be found inside of 'Bender Bending Rodriguez'
Changing the Name
// providing a different name
willy.define({
fn: function () {
return this.expected.indexOf(this.actual) > -1;
},
name: 'lieWithin'
});
will('potato').lieWithin('Bender Bending Rodriguez');
// expected 'potato' to lie within 'Bender Bending Rodriguez'
If you're lazy, use willy.loadDefinitions
to define multiple tests at a time.
willy.loadDefinitions({
beASubstringOf: {
fn: function () {
return this.expected.indexOf(this.actual) > -1;
},
explanation: 'be found inside of'
},
lieWithin: {
fn: function () {
return this.expected.indexOf(this.actual) > -1;
}
}
});
For supreme laziness, keep your custom tests in a Node module.
my-tests.js
exports.beASubstringOf = {
fn: function () {
return this.expected.indexOf(this.actual) > -1;
},
explanation: 'be found inside of'
};
exports.lieWithin = {
fn: function () {
return this.expected.indexOf(this.actual) > -1;
}
};
A Test Suite
var willy = require('willy'),
will = willy.will;
willy.loadDefinitions(require('./my-tests.js'));
describe('a test suite', function () {
it('should be pretty easy to share custom tests', function () {
will('potato').lieWithin('Bender Bending Rodriguez');
// expected 'potato' to lie within 'Bender Bending Rodriguez'
});
});