http://kostasbariotis.com
Kent Beck describes it as
Red
Green
Refactor
Just put a B instead of a T
describe('User', function() {
describe('#save()', function() {
it('should save without error', function(done) {
var user = new User('Luna');
user.save(function(err) {
if (err) throw err;
done();
});
});
});
});
var assert = require('assert');
describe('Array', function() {
describe('#indexOf()', function () {
it('should return -1 when the value is not present', function () {
assert.equal(-1, [1,2,3].indexOf(5));
assert.equal(-1, [1,2,3].indexOf(0));
});
});
});
The smallest unit of code that is testable.
should('abc').be.a.String();
a.should.be.eql(b);
this.obj.should.have.property('id').which.is.a.Number();
this.obj.should.have.property('path');
(10).should.be.within(0, 20);
(10).should.be.within(0, 20);
failedPromise().should.be.rejectedWith(Error, { message: 'boom' });
ISOLATED
FAST
it("calls the original function", function () {
var spy = sinon.spy();
var proxy = once(spy);
proxy();
assert(spy.called);
});
it("returns the return value from the original function", function () {
var stub = sinon.stub().returns(42);
var proxy = once(stub);
assert.equals(proxy(), 42);
});
it("returns the return value from the original function", function () {
var myAPI = { method: function () {} };
var mock = sinon.mock(myAPI);
mock.expects("method").once().returns(42);
var proxy = once(myAPI.method);
assert.equals(proxy(), 42);
mock.verify();
});
MULTIPLE UNITS WORKING TOGETHER
it("saves a User", function () {
var userModel = new User({
first_name: 'Kostas',
last_name: 'Bariotis'
})
userModel
.save()
.then(function(model){
db.collection('users')
.findOne({ last_name: 'Bariotis'})
.then(function(model) {
assert(model.first_name).equals('Kostas');
assert(model.last_name).equals('Bariotis');
})
})
});
var request = require('supertest');
describe('GET /user', function(){
it('respond with json', function(done){
request('http://localhost:3000')
.get('/user')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200, done);
})
})
var nock = require('nock');
var couchdb = nock('http://localhost:5984/')
.get('/users/1')
.reply(200, {
_id: '123ABC',
_rev: '946B7D1C',
username: 'kbariotis',
email: 'kbariotis@goodvid.io'
});
var Browser = require('zombie');
describe('User visits signup page', function() {
var browser = new Browser();
before(function(done) {
browser.visit('/signup', done);
});
describe('submits form', function() {
before(function(done) {
browser
.fill('email', 'zombie@underworld.dead')
.fill('password', 'eat-the-living')
.pressButton('Sign Me Up!', done);
});
it('should be successful', function() {
browser.assert.success();
});
it('should see welcome page', function() {
browser.assert.text('title', 'Welcome To Brains Depot');
});
});
});
If you liked my presentation, surely you'll like my tweets.
Check'em out at @kbariotis