Skip to content

Instantly share code, notes, and snippets.

@nullivex
Last active December 30, 2015 10:39
Show Gist options
  • Save nullivex/7817339 to your computer and use it in GitHub Desktop.
Save nullivex/7817339 to your computer and use it in GitHub Desktop.
Testing ActionHero; I used Mocha for the testing framework and Chai for the assertion library -- http://actionherojs.com
describe('Status Test',function(){
var action = require('../../actions/status').action
describe('Definition',function(){
it('should have a name',function(){
expect(action.name).to.equal('status')
})
it('should have a description',function(){
expect(action.description).to.equal('I will return some basic information about the API')
})
it('should have inputs',function(){
expect(action.inputs).to.be.an('object')
})
it('should have blocked connection types',function(){
expect(action.blockedConnectionTypes).to.be.instanceOf(Array)
})
it('should have valid example output',function(){
var out = action.outputExample
expect(out.status).to.equal('OK')
expect(out.uptime).to.equal(1234)
expect(Object.keys(out.stats).length).to.equal(0)
})
})
describe('Run',function(){
var api = require('../mocks/api')()
, connectionMock = require('../mocks/connection')()
action.run(api,connectionMock,function(connection){
console.log(connection)
it('should have an id',function(){
expect(connection.response.id).to.equal('1234')
})
it('should have a status',function(){
expect(connection.response.status).to.equal('OK')
})
it('should have a uptime',function(){
expect(connection.response.uptime).to.be.greaterThan(0)
})
it('should have stats',function(){
expect(Object.keys(connection.response.stats).length).to.be.greaterThan(0)
})
})
})
})
module.exports = function(){
return {
response: {}
}
}
module.exports = function(grunt){
//config
grunt.initConfig({
jshint: {
options: {
jshintrc: true,
reporter: require('jshint-stylish')
},
server: ['actions/*.js','initializers/*.js','tasks/*.js','test/*.js']
},
mochaTest: {
test: {
options: {
reporter: 'spec'
},
src: ['test/init.js','test/*.test.js','test/**/*.test.js']
}
}
})
//load tasks
grunt.loadNpmTasks('grunt-contrib-jshint')
grunt.loadNpmTasks('grunt-mocha-test')
//macros
grunt.registerTask('test',['jshint','mochaTest'])
}
/* global expect: true */
expect = require('chai').expect
var action = {}
/////////////////////////////////////////////////////////////////////
// metadata
action.name = 'status'
action.description = 'I will return some basic information about the API'
action.inputs = {
'required' : [],
'optional' : []
}
action.blockedConnectionTypes = []
action.outputExample = {
status: 'OK',
uptime: 1234,
stats: {}
}
/////////////////////////////////////////////////////////////////////
// functional
action.run = function(api, connection, next){
connection.response.id = api.id
connection.response.status = 'OK'
var now = new Date().getTime()
connection.response.uptime = now - api.bootTime
api.stats.getAll(function(err, stats){
connection.response.stats = stats
api.tasks.details(function(err, details){
connection.response.queues = details.queues
next(connection, true)
})
})
}
/////////////////////////////////////////////////////////////////////
// exports
exports.action = action
describe('Status Test',function(){
var action = require('../../actions/status').action
describe('Definition',function(){
it('should have a name',function(){
expect(action.name).to.equal('status')
})
it('should have a description',function(){
expect(action.description).to.equal('I will return some basic information about the API')
})
it('should have inputs',function(){
expect(action.inputs).to.be.an('object')
})
it('should have blocked connection types',function(){
expect(action.blockedConnectionTypes).to.be.instanceOf(Array)
})
it('should have valid example output',function(){
var out = action.outputExample
expect(out.status).to.equal('OK')
expect(out.uptime).to.equal(1234)
expect(Object.keys(out.stats).length).to.equal(0)
})
})
describe('Run',function(){
var api = require('../mocks/api')()
, connectionMock = require('../mocks/connection')()
action.run(api,connectionMock,function(connection){
it('should have an id',function(){
expect(connection.response.id).to.equal('1234')
})
it('should have a status',function(){
expect(connection.response.status).to.equal('OK')
})
it('should have a uptime',function(){
expect(connection.response.uptime).to.be.greaterThan(0)
})
it('should have stats',function(){
expect(Object.keys(connection.response.stats).length).to.be.greaterThan(0)
})
})
})
})

Testing ActionHero

I used Mocha for the testing framework and Chai for the assertion library.

See http://actionherojs.com for ActionHero information.

Folder Structure

  • test
    • actions
      • status.test.js
    • mocks
      • api.js
      • connection.js
    • tasks
    • init.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment