Testing Quick Start
Testing Quickstart Video
ColdBox tightly integrates with TestBox, the Behavior Driven Development Testing Framework for ColdFusion (CFML). We can easily do unit and integration testing for our application. To start, let's install TestBox via CommandBox. Please note that all ColdBox application templates have already TestBox installed as part of them, so you can skip this step.
1
install testbox --saveDev
Copied!
Please note the --saveDev flag we used. This tells CommandBox that this dependency is only for testing purposes and not for distribution or production usage. Check out the box.json under the devDependencies.

Test Harness

Every ColdBox application template comes with a pre-set testing harness under the /tests folder:
1
+ resources (Any testing helpers)
2
+ results (Any automated results go here)
3
+ specs (Your specs)
4
- Application.cfc
5
- runner.cfm
6
- test.xml
Copied!
Every harness has its own unique Application.cfc which must mimic your application's settings because the tests run in a different memory space. It also comes with an HTML runner called runner.cfm and an ANT runner called test.xml. All your test bundles and specifications will go under the specs directory and in the appropriate sub-directories:
1
+ integration
2
+ modules
3
+ unit
Copied!
Under the integration tests you will find the test bundles that come with the application template and the ones we generated:
1
+ MainSpec.cfc
Copied!
The MainSpec BDD test bundle can be show below:
1
/*******************************************************************************
2
* Integration Test as BDD
3
*
4
* Extends the integration class: coldbox.system.testing.BaseTestCase
5
*
6
* so you can test your ColdBox application headlessly. The 'appMapping' points by default to
7
* the '/root' mapping created in the test folder Application.cfc. Please note that this
8
* Application.cfc must mimic the real one in your root, including ORM settings if needed.
9
*
10
* The 'execute()' method is used to execute a ColdBox event, with the following arguments
11
* * event : the name of the event
12
* * private : if the event is private or not
13
* * prePostExempt : if the event needs to be exempt of pre post interceptors
14
* * eventArguments : The struct of args to pass to the event
15
* * renderResults : Render back the results of the event
16
*******************************************************************************/
17
component
18
extends ="coldbox.system.testing.BaseTestCase"
19
appMapping="/root"
20
{
21
22
/*********************************** LIFE CYCLE Methods ***********************************/
23
24
function beforeAll() {
25
super.beforeAll();
26
// do your own stuff here
27
}
28
29
function afterAll() {
30
// do your own stuff here
31
super.afterAll();
32
}
33
34
/*********************************** BDD SUITES ***********************************/
35
36
function run() {
37
describe( "Main Handler", function() {
38
beforeEach( function( currentSpec ) {
39
// Setup as a new ColdBox request, VERY IMPORTANT. ELSE EVERYTHING LOOKS LIKE THE SAME REQUEST.
40
setup();
41
} );
42
43
it( "can render the homepage", function() {
44
var event = this.get( "main.index" );
45
expect( event.getValue( name = "welcomemessage", private = true ) ).toBe( "Welcome to ColdBox!" );
46
} );
47
48
it( "can render some restful data", function() {
49
var event = this.post( "main.data" );
50
51
debug( event.getHandlerResults() );
52
expect( event.getRenderedContent() ).toBeJSON();
53
} );
54
55
it( "can do a relocation", function() {
56
var event = execute( event = "main.doSomething" );
57
expect( event.getValue( "relocate_event", "" ) ).toBe( "main.index" );
58
} );
59
60
it( "can startup executable code", function() {
61
var event = execute( "main.onAppInit" );
62
} );
63
64
it( "can handle exceptions", function() {
65
// You need to create an exception bean first and place it on the request context FIRST as a setup.
66
var exceptionBean = createMock( "coldbox.system.web.context.ExceptionBean" ).init(
67
erroStruct = structNew(),
68
extramessage = "My unit test exception",
69
extraInfo = "Any extra info, simple or complex"
70
);
71
prepareMock( getRequestContext() ).setValue(
72
name = "exception",
73
value = exceptionBean,
74
private = true
75
)
76
.$( "setHTTPHeader" );
77
78
// TEST EVENT EXECUTION
79
var event = execute( "main.onException" );
80
} );
81
82
describe( "Request Events", function() {
83
it( "fires on start", function() {
84
var event = execute( "main.onRequestStart" );
85
} );
86
87
it( "fires on end", function() {
88
var event = execute( "main.onRequestEnd" );
89
} );
90
} );
91
92
describe( "Session Events", function() {
93
it( "fires on start", function() {
94
var event = execute( "main.onSessionStart" );
95
} );
96
97
it( "fires on end", function() {
98
// Place a fake session structure here, it mimics what the handler receives
99
URL.sessionReference = structNew();
100
URL.applicationReference = structNew();
101
var event = execute( "main.onSessionEnd" );
102
} );
103
} );
104
} );
105
}
106
107
}
108
Copied!

Executing The Runner

To execute your application template tests and the generated tests just browse to the URL: http://127.0.0.1:{port}/tests/runner.cfm and you will get a full integration report:
Test Runner
Everything is already pre-wired for you and ready for you to do full life-cycle integration testing. This means that upon first request to the spec, a virtual ColdBox application will load and setup your application from where you can test any part of it. From executing events just like from the browser, or execution API requests just like any application.
Sounds too good to be true? Let's try this out. Open the handlers/Main.cfc and look for the index() action. Let's change it to this:
1
function index( event, rc, prc ) {
2
prc.welcomeMessage = "Welcome to ColdBox!";
3
event.setFunkyView( "main/index" );
4
}
Copied!
Now execute the tests again? What do you see?
You will get an error now: component [coldbox.system.web.context.RequestContext] has no function with name [setFunkyView]. Fix it and re-run it. Ok, hold on to something..... You are now doing live integration testing my friend. Simple, but yet accomplishing.

CommandBox Runner

You can also execute the runner via CommandBox. So let's tell CommandBox where your runner is located, just change the {port} to your port assigned.
1
package set testbox.runner=http://127.0.0.1:{port}/tests/runner.cfm
Copied!
Then use the testbox run command:
1
testbox run
Copied!
CommandBox TestBox Runner
You will then execute your tests and get a text report from it. If you want CommandBox to watch for changes to your source code and THEN re-execute the tests, then start a watcher:
1
testbox watch
Copied!
Tip: Run a testbox run ? and a testbox watch ? to get help on these wonderful commands. They have tons of other arguments you can use.

What's Next

We have a fully dedicated section on testing, please visit it for in-depth information.
Last modified 1yr ago