Geddy Unit Test Utilities
A support library that helps with unit testing Geddy applications.
Installation
> npm install geddy-unit-test-utilities
Features
- Write tests that hit controllers and actions just like a user request would
- Test framework agnostic -- use with Mocha, Geddy's
jake test
, or any other test framework - Test templates and views from command line
- Sticky sessions -- test chains of requests rather than just one
- Sticky data -- pass data from one test to another
Compatibility
- 1.1.x works with Geddy 13+
- 1.0.x works with Geddy 0.11.x
Examples
config/router.js
routing configuration
Process request using var assert = require( 'assert' ),
GeddyUnitTestUtilities = require( 'geddy-unit-test-utilities' );
var tests = {
'test routing' : function( next )
{
GeddyUnitTestUtilities.router.route(
{
// request URL, required
url : '/path/to/my/action?id=1234',
// optional request method, defaults to GET
method : 'POST',
// optional request parameters passed to the controller
params : {
name : 'John',
phone : '12345678'
},
// optional request headers
headers : {
accept : 'application/json'
}
},
function( response )
{
// specify what happens when request has been processed
console.log( response.getStatusCode() );
console.log( response.getHeader( 'content-type' ) );
console.log( response.getContent() );
assert.equals( response.getStatusCode(), 200 );
next();
}
);
}
};
module.exports = tests;
Run specific action from specific controller
var assert = require( 'assert' ),
GeddyUnitTestUtilities = require( 'geddy-unit-test-utilities' );
var tests = {
'test controller action' : function( next )
{
// specify controller
var ctl = GeddyUnitTestUtilities.controller.create( 'MyTestController' );
// optional -- specify parameters passed to the controller
ctl.setParams(
{
id : 1234,
name : 'John',
phone : '12345678'
}
);
// optional -- specify request headers
ctl.setHeaders(
{
accept : 'application/json'
}
);
// optional -- set request URL
ctl.setUrl( '/hello/world' );
// optional -- set request method
ctl.setMethod( 'POST' );
// specify what happens when request has been processed
ctl.response.onComplete(
function( response )
{
console.log( response.getStatusCode() );
console.log( response.getHeader( 'content-type' ) );
console.log( response.getContent() );
assert.equals( response.getStatusCode(), 200 );
next();
}
);
// run action
ctl.unit.runAction( 'myAction' );
}
};
module.exports = tests;
Sticky sessions
In order to maintain a single session across multiple queries, specify the following in the before()
and after()
functions of your test. Note that the mock queries do not respect cookie expirations or paths; all cookies returned
are passed to the next request as-is.
If session.end()
is not called in the after()
function, the sessions will stick across test modules.
var assert = require( 'assert' ),
GeddyUnitTestUtilities = require( 'geddy-unit-test-utilities' );
var tests = {
'before' : function( next )
{
// enable sticky session
GeddyUnitTestUtilities.session.start( next );
},
'after' : function( next )
{
// disable sticky session
GeddyUnitTestUtilities.session.end();
next();
}
// ... ( your tests go here )
};
module.exports = tests;
Cookie manipulation
Cookies from previous request (a.k.a. cookies that will be passed to the next request) are accessible via
GeddyUnitTestUtilities.session.cookieStore
.
console.log( GeddyUnitTestUtilities.session.cookieStore.get( 'sid' ) );
GeddyUnitTestUtilities.session.cookieStore.set( 'sid', 'Hello world' );
Sticky data
Test utilies provide a way to store data across tests and test modules.
var assert = require( 'assert' ),
GeddyUnitTestUtilities = require( 'geddy-unit-test-utilities' );
var tests = {
'test first' : function( next )
{
// store sticky data
GeddyUnitTestUtilities.global.set( 'myVariable', 'Hello world' );
next();
},
'test second' : function( next )
{
// retrieve sticky data
console.log( GeddyUnitTestUtilities.global.get( 'myVariable' ) );
next();
}
'after' : function( next )
{
// empty sticky data
GeddyUnitTestUtilities.global.reset();
next();
}
};
module.exports = tests;
Helper functions
GeddyUnitTestUtilities.helper.getBasicAuthStr( username, password )
Returns a string which can be used as basic HTTP authentication in the Authorization
HTTP request header.
Notes
Version 1.1.x is compatible with Geddy 13+.
Version 1.0.x is compatible with Geddy 0.11.x.
If you wish to use templates during testing with Geddy 0.11.x, a small change to original Geddy source code is required, available here.