Skip to content

Commit cc85c5e

Browse files
lmajanogitbook-bot
authored andcommitted
GitBook: [master] 19 pages modified
1 parent 80d9062 commit cc85c5e

19 files changed

+435
-32
lines changed

SUMMARY.md

-1
Original file line numberDiff line numberDiff line change
@@ -282,5 +282,4 @@
282282
* [Dynamic Testing](architecture-concepts/testing-concepts/dynamic-testing.md)
283283
* [Developer Focus](architecture-concepts/testing-concepts/developer-focus.md)
284284
* [Testing Vocabulary](architecture-concepts/testing-concepts/testing-vocabulary.md)
285-
* [Testing Tools](architecture-concepts/testing-concepts/testing-tools.md)
286285

architecture-concepts/testing-concepts/testing-tools.md

-15
This file was deleted.

digging-deeper/recipes/application-templates.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ We have a Github organization called [coldbox-templates](https://github.com/cold
66

77
The `coldbox create app` command has integration to our application templates via the `skeleton` argument. This can be the name of each of the templates in our repositories or you can use the following alternatives:
88

9-
* Name of template in our organization: `advanced,simple,super-simple,etc`
9+
* Name of template in our organization: `advanced,simple,super-simple,rest, rest-hmvc, vuejs, etc`
1010
* A name of a ForgeBox entry: `cbtemplate-advanced-script,cbtemplate-simple`
1111
* A Github shortcut: `github-username/repo`
12-
* An HTTP/S URL to a zip file containing a template: \`[http://myapptemplates.com/template.zip](http://myapptemplates.com/template.zip)
12+
* An HTTP/S URL to a zip file containing a template: [http://myapptemplates.com/template.zip](http://myapptemplates.com/template.zip)
1313
* A folder containing a template: `/opt/shared/templates/my-template`
1414
* A zip file containing the template: `/opt/shared/templates/my-template.zip`
1515

testing/testing-coldbox-applications/README.md

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
# Testing ColdBox Applications
22

3-
One of the best things you can do when you develop software applications is TEST! I know nobody likes it, but hey, you need to do it right? With the advocation of frameworks today, you get all these great tools to build your software applications, but how do you test your framework code. ColdBox has revolutionized testing MVC and framework code, since you can unit test your event handlers, interceptors, model objects and even do integration testing and test your entire application with no browser at all.
3+
One of the best things you can do when you develop software applications is TEST! I know nobody likes it, but hey, you need to do it right? With the advocation of frameworks today, you get all these great tools to build your software applications, but how do you test your framework code. ColdBox has revolutionized testing HMVC and framework code, since you can unit test your event handlers, interceptors, model objects and even do **integration testing and true BDD \(Behavior Driven Development\)** and test your entire application with no browser at all.
44

55
ColdBox has already all the hooks in place to provide Behavior and Test Driven Development via [TestBox](http://www.ortussolutions.com/products/testbox) and mocking/stubbing capabilities via MockBox.
66

7-
> TestBox is a testing framework for ColdFusion \(CFML\) that is based on BDD \(Behavior Driven Development\) for providing a clean obvious syntax for writing tests. It also includes MockBox for mocking and stubbing.
7+
{% hint style="info" %}
8+
**TestBox** is a testing framework for ColdFusion \(CFML\) that is based on BDD \(Behavior Driven Development\) for providing a clean obvious syntax for writing tests. It also includes MockBox for mocking and stubbing.
9+
10+
**TestBox is included with all ColdBox templates.**
11+
{% endhint %}
812

913
## Installing TestBox
1014

11-
So let's get started by opening a CommandBox prompt \(`box`\) in your application root and installing TestBox.
15+
TestBox already comes defined in the `box.json` of all ColdBox application templates. However, if you have a custom ColdBox app or a custom template, then you can easily install it via CommandBox:
1216

1317
```bash
1418
// stable
@@ -18,7 +22,9 @@ install testbox --saveDev
1822
install testbox@be --saveDev
1923
```
2024

21-
> **Info** The `--saveDev` flag tells CommandBox to save TestBox locally only for testing purposes as it will not be used to send TestBox for production \([http://commandbox.ortusbooks.com/content/packages/dependencies.html](http://commandbox.ortusbooks.com/content/packages/dependencies.html)\)
25+
{% hint style="info" %}
26+
The --saveDev flag tells CommandBox to save TestBox locally only for testing purposes as it will not be used to send TestBox for production \([http://commandbox.ortusbooks.com/content/packages/dependencies.html](http://commandbox.ortusbooks.com/content/packages/dependencies.html)\)
27+
{% endhint %}
2228

2329
Please also note that CommandBox ships with tons of commands for interacting with TestBox and ColdBox testing classes. You can explore them by issuing the following commands or viewing the latest CommandBox API Docs \([http://apidocs.ortussolutions.com/commandbox/current](http://apidocs.ortussolutions.com/commandbox/current)\)
2430

@@ -32,6 +38,9 @@ testbox create help
3238
# run tests
3339
testbox run help
3440

41+
# Watch your files and execute the tests if they change
42+
testbox watch
43+
3544
# coldbox creation help
3645
coldbox create help
3746
```
@@ -47,11 +56,12 @@ It might be that testing is tedious and takes time to get into the flow of Behav
4756
* Encourages refactoring to produce testable code
4857
* Testing is all about behavior and expectations
4958

59+
**Our recommendation is to do BDD and integration testing first. Unit testing is important but it is more important to verify that your requirements are met.**
60+
5061
## Resources
5162

5263
* [Wikipedia](http://en.wikipedia.org/wiki/Unit_test) - [http://en.wikipedia.org/wiki/Unit\_test](http://en.wikipedia.org/wiki/Unit_test)
5364
* [TestBox](http://ortussolutions.com/products/testbox) - [http://ortussolutions.com/products/testbox](http://ortussolutions.com/products/testbox)
54-
* [TestBox BDD Book](http://testbox.ortusbooks.com) - [http://testbox.ortusbooks.com](http://testbox.ortusbooks.com)
5565
* [ColdFusion Builder TestBox Extensions](http://forgebox.io/view/coldbox-platform-utilities) - [http://forgebox.io/view/coldbox-platform-utilities](http://forgebox.io/view/coldbox-platform-utilities)
5666
* [Sublime TestBox Extension](https://github.com/lmajano/cbox-coldbox-sublime) - [https://github.com/lmajano/cbox-coldbox-sublime](https://github.com/lmajano/cbox-coldbox-sublime)
5767
* [VSCode TestBox Extension](https://marketplace.visualstudio.com/items?itemName=ortus-solutions.vscode-testbox) - [https://marketplace.visualstudio.com/items?itemName=ortus-solutions.vscode-testbox](https://marketplace.visualstudio.com/items?itemName=ortus-solutions.vscode-testbox)

testing/testing-coldbox-applications/coldbox-testing-classes.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Before we begin our adventures in testing, let's review what classes does ColdBo
66

77
From that super class we have our own ColdBox `BaseTestCase` which is our base class for any testing in ColdBox and the class used for Integration Testing. We then spawn several child classes for targeted testing of different objects in your ColdBox applications:
88

9-
* `BaseTestCase` - Used for Integration Testing
10-
* `BaseModelTest` - Used for model object testing
11-
* `BaseInterceptorTest` - Used for interceptor testing
12-
* `BaseHandlerTest` - Used for isolated handler testing
9+
* `BaseTestCase` - Used for **Integration Testing**
10+
* `BaseModelTest` - Used for model object unit testing
11+
* `BaseInterceptorTest` - Used for interceptor unit testing
12+
* `BaseHandlerTest` - Used for isolated handler unit testing
1313

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,61 @@
11
# Integration Testing
22

3+
## Integration Testing
34

5+
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.
6+
7+
8+
9+
### Basics
10+
11+
[![](https://github.com/ortus-docs/coldbox-docs/raw/master/full/images/HandlerToTestRelationship.png)](https://github.com/ortus-docs/coldbox-docs/blob/master/full/images/HandlerToTestRelationship.png)
12+
13+
Here are the basics to follow for integration testing:
14+
15+
* Create one test bundle CFC for each event handler you would like to test or base it off your BDD requirements
16+
* Bundle CFC inherits from `coldbox.system.testing.BaseTestCase`
17+
* The bundle CFC can have some annotations that tell the testing framework to what ColdBox application to connect to and test
18+
* Execution of the event is done via the `execute()` method, which returns a request context object
19+
* Most verifications and assertions are done via the contents of the request context object \(request collections\)
20+
21+
```text
22+
/**
23+
* My integration test bundle
24+
*/
25+
component extends="coldbox.system.testing.BaseTestCase" appMapping="/root"{
26+
27+
/*********************************** LIFE CYCLE Methods ***********************************/
28+
29+
function beforeAll(){
30+
super.beforeAll();
31+
// do your own stuff here
32+
}
33+
34+
function afterAll(){
35+
// do your own stuff here
36+
super.afterAll();
37+
}
38+
39+
/*********************************** BDD SUITES ***********************************/
40+
41+
function run(){
42+
43+
}
44+
```
45+
46+
We will explain later the life-cycle methods and the `run()` method where you will be writing your specs.
47+
48+
> **Hint** Please refer to our BDD primer to start: [http://testbox.ortusbooks.com/content/primers/bdd/index.html](http://testbox.ortusbooks.com/content/primers/bdd/index.html)
49+
50+
### CommandBox Generation
51+
52+
Also remember that you can use CommandBox to generate integration tests with a few simple commands:
53+
54+
```text
55+
coldbox create integration-test handler=main actions=index,save,run --open
56+
# help
57+
coldbox create integration-test help
58+
```
59+
60+
> **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.
461
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,44 @@
11
# Common Methods
22

3+
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](http://testbox.ortusbooks.com/content/test_bundles/index.html)\):
34

5+
```text
6+
// quick assertion methods
7+
assert( expression, [message] )
8+
fail(message)
9+
10+
// bdd methods
11+
beforeEach( body )
12+
afterEach( body )
13+
describe( title, body, labels, asyncAll, skip )
14+
xdescribe( title, body, labels, asyncAll )
15+
it( title, body, labels, skip )
16+
xit( title, body, labels )
17+
expect( actual )
18+
19+
// extensions
20+
addMatchers( matchers )
21+
addAssertions( assertions )
22+
23+
// runners
24+
runRemote( testSpecs, testSuites, deubg, reporter );
25+
26+
// utility methods
27+
console( var, top )
28+
debug( var, deepCopy, label, top )
29+
clearDebugBuffer()
30+
getDebugBuffer()
31+
print( message )
32+
printLn( message )
33+
34+
// mocking methods
35+
makePublic( target, method, newName )
36+
querySim( queryData )
37+
getmockBox( generationPath )
38+
createEmptyMock( className, object, callLogging )
39+
createMock( className, object, clearMethods, callLogging )
40+
prepareMock( object, callLogging )
41+
createStub( callLogging, extends, implements )
42+
getProperty( target, name, scope, defaultValue )
43+
```
444

testing/testing-coldbox-applications/integration-testing/handler-returning-results.md

+25
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,28 @@
22

33

44

5+
## Handler Returning Results
6+
7+
If you have written event handlers that actually return data, then you will have to get the values from the request collection. The following are the keys created for you in the request collection.
8+
9+
| Key | Description |
10+
| :--- | :--- |
11+
| `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. |
12+
13+
**The handler** \(`main.cfc`\)
14+
15+
```text
16+
function list(event,rc,prc){
17+
return "Hola Luis";
18+
}
19+
```
20+
21+
**The spec**
22+
23+
```text
24+
it( "+homepage renders", function(){
25+
var event = execute( event="main.list", renderResults=true );
26+
expect( event.getValue( name="cbox_handler_results" ) ).toBe( "Hola Luis" );
27+
});
28+
```
29+

testing/testing-coldbox-applications/integration-testing/http-method-mocking.md

+23
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,26 @@
22

33

44

5+
## HTTP Method Mocking
6+
7+
There are times when building RESTFul web services that you need to mock the incoming HTTP verb. This is done via MockBox:
8+
9+
```text
10+
function run(){
11+
12+
describe( "Main Handler", function(){
13+
14+
beforeEach(function( currentSpec ){
15+
setup();
16+
});
17+
18+
it( "+homepage renders", function(){
19+
prepareMock( getRequestContext() )
20+
.$( "getHTTPMethod", "DELETE" );
21+
FORM.userID = 123;
22+
var event = execute( event = "user.delete" );
23+
});
24+
});
25+
}
26+
```
27+
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,36 @@
11
# Life-Cycle Events
22

3+
[![](https://github.com/ortus-docs/coldbox-docs/raw/master/full/images/testing-lifecycle.png)](https://github.com/ortus-docs/coldbox-docs/blob/master/full/images/testing-lifecycle.png)
34

5+
ColdBox testing leverages TestBox's testing life-cycle events \([http://testbox.ortusbooks.com/content/life-cycle\_methods/index.html](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()`.
6+
7+
```text
8+
function beforeAll(){
9+
super.beforeAll();
10+
// do your own stuff here
11+
}
12+
13+
function afterAll(){
14+
// do your own stuff here
15+
super.afterAll();
16+
}
17+
```
18+
19+
> **Important** If you override any of these methods and do not funnel the super call, then you might get cached or unexpected results.
20+
21+
### Improving Performance: Unloading ColdBox
22+
23+
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.
24+
25+
```text
26+
component extends="coldbox.system.testing.BaseTestCase" unloadColdBox=false{
27+
28+
29+
function beforeAll(){
30+
this.unloadColdBox = false;
31+
super.beforeAll();
32+
// do your own stuff here
33+
}
34+
}
35+
```
436

testing/testing-coldbox-applications/integration-testing/rendering-results.md

+22
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,25 @@
22

33

44

5+
## Rendering Results
6+
7+
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.
8+
9+
```text
10+
it( "can render users", function(){
11+
var event = execute( event="rest.api.users", renderResults=true );
12+
expect( event.getValue( "cbox_rendered_content" ) ).toBeJSON();
13+
} );
14+
```
15+
16+
### `getRenderedContent()`
17+
18+
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.
19+
20+
```text
21+
it( "can render users", function(){
22+
var event = execute( event="rest.api.users", renderResults=true );
23+
expect( event.getRenderedContent() ).toBeJSON();
24+
} );
25+
```
26+
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
# Test Annotations
22

3+
Here are the annotations you can add to your testing bundle CFC.
34

5+
| Annotation | Type | Required | Default | Description |
6+
| :--- | :--- | :--- | :--- | :--- |
7+
| `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. |
8+
| `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. |
9+
| `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. |
10+
| `loadColdBox` | boolean | false | true | By default the base test case will load the virtual application into the `application`scope so all specs can execute |
11+
| `unloadColdBox` | boolean | false | true | The base test case will unload the virtual application from the `application` scope after all specs have executed. |
12+
13+
**Examples**
14+
15+
```text
16+
component extends="coldbox.system.testing.BaseTestCase" appMapping="/apps/MyApp"{}
17+
18+
component extends="coldbox.system.testing.BaseTestCase"
19+
appMapping="/apps/MyApp"
20+
configMapping="apps.MyApp.test.resources.Config"{}
21+
```
22+
23+
> **Caution** The `AppMapping` setting is the most important one. This is how your test connects to a location of a ColdBox application to test.
424
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
11
# Test Setup
22

3+
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.
34

5+
```text
6+
component extends="coldbox.system.testing.BaseTestCase" appMapping="/apps/MyApp"{
7+
8+
function run(){
9+
10+
describe( "Main Handler", function(){
11+
12+
beforeEach(function( currentSpec ){
13+
// Setup as a new ColdBox request, VERY IMPORTANT. ELSE EVERYTHING LOOKS LIKE THE SAME REQUEST.
14+
setup();
15+
});
16+
17+
it( "+homepage renders", function(){
18+
var event = execute( event="main.index", renderResults=true );
19+
expect( event.getValue( name="welcomemessage", private=true ) ).toBe( "Welcome to ColdBox!" );
20+
});
21+
22+
});
23+
}
24+
25+
}
26+
```
427

0 commit comments

Comments
 (0)