Skip to content

Instantly share code, notes, and snippets.

@Schoonology
Last active May 13, 2016 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Schoonology/a5c8fa03da6a6fba68a9770c88b14498 to your computer and use it in GitHub Desktop.
Save Schoonology/a5c8fa03da6a6fba68a9770c88b14498 to your computer and use it in GitHub Desktop.
TestDouble + Ava Controller Test

I'm using AVA for the test runner, and testdouble for mocking. Glueing them together to test an Express route handler was completely straightforward, and worked the first time. Here's the gist of that.

Why a "controller test"?

My spec/E2E tests involve spinning up a Docker container, but—for the time being—if the application fails on start-up, the spec script (powered by Scripty, #onbrand) fails silently, which is annoying. To quickly solve my problem, I isolated the only controller I changed into a unit test. Normally, this isn't a great idea, as it's duplicating work and it can make it harder to change implementation details.

var express = require('express')
var test = require('ava')
var td = require('testdouble')
var meta = require('../../../lib/controllers/meta')
test(t => {
t.truthy(typeof meta === 'object')
})
test(t => {
t.truthy(typeof meta.upcheck === 'function')
})
test(t => {
var res = td.object(express.response)
// This allows chaining, if that's your thing.
td.when(res.status(td.matchers.anything())).thenReturn(res)
meta.upcheck(null, res)
td.verify(res.status(204))
td.verify(res.send())
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment