Every ColdBox application template comes with a nice test harness inside of a tests
folder.
Test Harness
File/Folder | Description |
---|---|
Application.cfc | A unique application file for your test harness. It should mimic exactly the one in your root application folder |
resources | Some basic testing resources or any of your own testing resources |
runner.cfm | The HTML runner for your test bundles |
specs | Where you will write your testing bundle specs for integration, unit, and module testing, try to use the convention of {name}Spec.cfc or {name}Test.cfc |
test.xml | A TestBox ANT runner |
The Application.cfc
for your tests is extremely important as it should mimic your applications real Application.cfc
.
/**
* Copyright 2005-2007 ColdBox Framework by Luis Majano and Ortus Solutions, Corp
* www.ortussolutions.com
* ---
*/
component {
// APPLICATION CFC PROPERTIES
this.name = "ColdBoxTestingSuite";
this.sessionManagement = true;
this.setClientCookies = true;
this.sessionTimeout = createTimespan( 0, 0, 15, 0 );
this.applicationTimeout = createTimespan( 0, 0, 15, 0 );
this.whiteSpaceManagement = "smart";
this.enableNullSupport = shouldEnableFullNullSupport();
/**
* --------------------------------------------------------------------------
* Location Mappings
* --------------------------------------------------------------------------
* - cbApp : Quick reference to root application
* - coldbox : Where ColdBox library is installed
* - testbox : Where TestBox is installed
*/
// Create testing mapping
this.mappings[ "/tests" ] = getDirectoryFromPath( getCurrentTemplatePath() );
// The root application mapping
rootPath = reReplaceNoCase( this.mappings[ "/tests" ], "tests(\\|/)", "" );
this.mappings[ "/root" ] = this.mappings[ "/cbapp" ] = rootPath;
this.mappings[ "/coldbox" ] = rootPath & "coldbox";
this.mappings[ "/testbox" ] = rootPath & "testbox";
/**
* Fires on every test request. It builds a Virtual ColdBox application for you
*
* @targetPage The requested page
*/
public boolean function onRequestStart( targetPage ){
// Set a high timeout for long running tests
setting requestTimeout ="9999";
// New ColdBox Virtual Application Starter
request.coldBoxVirtualApp= new coldbox.system.testing.VirtualApp( appMapping = "/root" );
// If hitting the runner or specs, prep our virtual app
if ( getBaseTemplatePath().replace( expandPath( "/tests" ), "" ).reFindNoCase( "(runner|specs)" ) ) {
request.coldBoxVirtualApp.startup();
}
// Reload for fresh results
if ( structKeyExists( url, "fwreinit" ) ) {
if ( structKeyExists( server, "lucee" ) ) {
pagePoolClear();
}
// ormReload();
request.coldBoxVirtualApp.restart();
}
return true;
}
/**
* Fires when the testing requests end and the ColdBox application is shutdown
*/
public void function onRequestEnd( required targetPage ){
request.coldBoxVirtualApp.shutdown();
}
private boolean function shouldEnableFullNullSupport(){
var system = createObject( "java", "java.lang.System" );
var value = system.getEnv( "FULL_NULL" );
return isNull( value ) ? false : !!value;
}
}
Please note that we provide already a mapping to your root application via /root
and a mapping to the tests themselves via /tests
. We recommend you add any ORM specs or any other mappings here.
{% hint style="success" %}
Tip: Make sure all the same settings and configs from your root Application.cfc
are replicated in your tests Application.cfc
{% endhint %}