Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Here is a spec written for you. Please note that in the beforeEach()
life-cycle method you need to execute the setup()
method will will setup a new ColdBox request for each spec you run.
The base test cases all inherit from TestBox so here are a few of the common methods you can find in every test bundle CFC (http://testbox.ortusbooks.com/content/test_bundles/index.html):
ColdBox testing leverages TestBox's testing life-cycle events (http://testbox.ortusbooks.com/content/life-cycle_methods/index.html) in order to prepare the virtual ColdBox application, request context and then destroy it. For performance, a virtual application is loaded for all test cases contained within a test bundle CFC via the beforeAll()
and destroyed under afterAll()
.
Important If you override any of these methods and do not funnel the super call, then you might get cached or unexpected results.
The default for integration testing is that the virtual ColdBox application will be destroyed or unloaded in each test. To keep the virtual application accross requests you will need to use the unloadColdBox=false
annotation or the this.unloadColdBox=false
setting in your beforeAll()
method. This will stop the testing classes from destroying ColdBox and improving performance.
Here are the annotations you can add to your testing bundle CFC.
Examples
Caution The
AppMapping
setting is the most important one. This is how your test connects to a location of a ColdBox application to test.
The execute()
method is your way of making requests in to your ColdBox application. It can take the following parameters:
We will begin our adventure with integration testing. Integration testing allows you to test a real life request to your application without using a browser. Your test bundle CFC will load a new virtual application for you to test each specification under it; all aspects of your application are loaded: caching, dependency injection, AOP, etc. Then you can target an event to test and verify its behavior accordingly. First of all, these type of tests go in your integration
folder of your test harness or in the specific module folder if you are testing modules.
Here are the basics to follow for integration testing:
Create one test bundle CFC for each event handler you would like to test or base it off your BDD requirements
Bundle CFC inherits from coldbox.system.testing.BaseTestCase
The bundle CFC can have some annotations that tell the testing framework to what ColdBox application to connect to and test
Execution of the event is done via the execute()
method, which returns a request context object
Most verifications and assertions are done via the contents of the request context object (request collections)
We will explain later the life-cycle methods and the run()
method where you will be writing your specs.
Hint Please refer to our BDD primer to start: http://testbox.ortusbooks.com/content/primers/bdd/index.html
Also remember that you can use CommandBox to generate integration tests with a few simple commands:
Info Please also note that whenever you create a handler, interceptor or model with CommandBox it will automatically create the integration or unit test for you.
Let's use a sample event handler so we can test it:
I can test this entire handler without me building any views yet. I can even test the relocations that happen via setNextEvent()
. ColdBox will wire itself up with some mocking classes to intercept those relocations for you and place those values in the request collection for you so you can assert them. It creates a key called setnextevent in the request collection and any arguments passed to the method are also saved as keys with the following pattern:
Caution Any relocation produced by the framework via the
setNextEvent
method will produce some variables in the request collection for you to verify relocations.
Here is the integration test for the Main
handler.
Annotation
Type
Required
Default
Description
appMapping
string
false
/
The application mapping of the ColdBox application to test. By defaults it maps to the root. Extermely important this mapping is a slash notation that points to the root of the ColdBox application to test.
configMapping
string
false
{appMapping}/config/Coldbox.cfc
The configuration file to load for this test, which by convention uses the same configuration as the application uses. This is a dot notation path to a configuration CFC.
coldboxAppKey
string
false
cbController
The named key of the ColdBox controller that will be placed in application scope for you to simulate the ColdBox application. Used mostly on advanced testing cases where you have altered the default application key.
loadColdBox
boolean
false
true
By default the base test case will load the virtual application into the application
scope so all specs can execute
unloadColdBox
boolean
false
true
The base test case will unload the virtual application from the application
scope after all specs have executed.
Parameter Name
Parameter Type
Required
Default Value
Description
event
string
false
""
The event name to execute. e.g., "Main.index"
. Either an event or a route must be provided.
route
string
false
""
The route to execute. e.g., "/login"
. The route parameter appends to your default SES Base Url set in config/routes.cfm
. Either an event or a route must be provided.
queryString
string
false
""
Any parameters to be passed as a query string. These will be added to the Request Context for the test request.
private
boolean
false
false
If true
, sets the event execution as private.
prePostExempt
boolean
false
false
If true
, skips the pre- and post- interceptors. e.g., preEvent
, postHandler
, etc.
eventArguments
struct
false
{}
A collection of arguments to passthrough to the calling event handler method.
renderResults
struct
false
false
If true, then it will try to do the normal rendering procedures and store the rendered content in the RC as cbox_rendered_content
.
withExceptionHandling
boolean
false
false
If true, then ColdBox will process any errors through the exception handling framework instead of just throwing the error.
The BaseTestCase
leverages an internal virtual ColdBox application so you can do integration testing. Meaning that whenever you extend from the BaseTestCase
the virtual ColdBox will be loaded. You can tell the testing framework to NOT load it by using the this.loadColdBox
variable:
That's it! You will be able to use anything from the BaseTestCase
but the virtual application will not be loaded.
The execute()
method has an argument called renderResults
which defaults to false. If you pass in true then ColdBox will go through the normal rendering procedures and save the results in a request collection variable called: cbox_rendered_content
and expose to you a method in the request context called getRenderedContent()
. It will even work with renderData()
or if you are returning RESTful information. Then you can easily assert what the content would have been for an event.
getRenderedContent()
In testing mode, the ColdBox request context event
object has a convenience method called getRenderedContent()
that will give you the rendered content instead of you going directly to the request context and finding the cbox_rendered_content
variable.
Key
Description
cbox_handler_results
The value returned from the event handler method. This key will NOT be created if the handler does not return any data.