Assertions
Assertions are self-concatenated strings that evaluate an actual value to an expected value or condition. These are initiated by the global TestBox variable called $assert which contains tons of included assertion methods so you can evaluate your tests.

Evaluators

Each assertion evaluator will compare the actual value and an expected value or condition. It is responsible for either passing or failing this evaluation and reporting it to TestBox. Each evaluator also has a negative counterpart assertion by just prefixing the call to the method with a not expression.
1
function testIncludes(){
2
$assert.includes( "hello", "HE" );
3
$assert.includes( [ "Monday", "Tuesday" ] , "monday" );
4
}
5
6
function testNotIncludes(){
7
$assert.notIncludes( "hello", "what" );
8
$assert.notIncludes( [ "Monday", "Tuesday" ] , "Friday" );
9
}
Copied!

Included Evaluators

TestBox has a plethora (That's Right! I said Plethora) of evaluators that are included in the release. The best way to see all the latest evaluator methods is to visit our API and digest the coldbox.system.Assertion class. There is also the ability to register and write custom assertion evaluators in TestBox via our addAssertions() function.
1
component displayName="TestBox xUnit suite for CF9" labels="railo,cf"{
2
3
/*********************************** LIFE CYCLE Methods ***********************************/
4
5
function beforeTests(){
6
application.salvador = 1;
7
}
8
9
function afterTests(){
10
structClear( application );
11
}
12
13
function setup(){
14
request.foo = 1;
15
}
16
17
function teardown(){
18
structClear( request );
19
}
20
21
/*********************************** Test Methods ***********************************/
22
23
function testIncludes(){
24
$assert.includes( "hello", "HE" );
25
$assert.includes( [ "Monday", "Tuesday" ] , "monday" );
26
}
27
28
function testIncludesWithCase(){
29
$assert.includesWithCase( "hello", "he" );
30
$assert.includesWithCase( [ "Monday", "Tuesday" ] , "Monday" );
31
}
32
33
function testnotIncludesWithCase(){
34
$assert.notincludesWithCase( "hello", "aa" );
35
$assert.notincludesWithCase( [ "Monday", "Tuesday" ] , "monday" );
36
}
37
38
function testNotIncludes(){
39
$assert.notIncludes( "hello", "what" );
40
$assert.notIncludes( [ "Monday", "Tuesday" ] , "Friday" );
41
}
42
43
function testIsEmpty(){
44
$assert.isEmpty( [] );
45
$assert.isEmpty( {} );
46
$assert.isEmpty( "" );
47
$assert.isEmpty( queryNew("") );
48
}
49
50
function testIsNotEmpty(){
51
$assert.isNotEmpty( [1,2] );
52
$assert.isNotEmpty( {name="luis"} );
53
$assert.isNotEmpty( "HelloLuis" );
54
$assert.isNotEmpty( querySim( "id, name
55
1 | luis") );
56
}
57
58
function testSkipped() skip{
59
$assert.fail( "This Test should fail" );
60
}
61
62
boolean function isRailo(){
63
return structKeyExists( server, "railo" );
64
}
65
66
function testSkippedWithConstraint() skip="isRailo"{
67
$assert.fail( "This Test should fail" );
68
}
69
70
function testFails(){
71
//$assert.fail( "This Test should fail" );
72
}
73
74
function testFailsShortcut() labels="railo"{
75
//fail( "This Test should fail" );
76
}
77
78
function testAssert() {
79
$assert.assert( application.salvador == 1 );
80
}
81
82
function testAssertShortcut() {
83
assert( application.salvador == 1 );
84
}
85
86
function testisTrue() {
87
$assert.isTrue( 1 );
88
}
89
90
function testisFalse() {
91
$assert.isFalse( 0 );
92
}
93
94
function testisEqual() {
95
$assert.isEqual( 0, 0 );
96
$assert.isEqual( "hello", "HEllO" );
97
$assert.isEqual( [], [] );
98
$assert.isEqual( [1,2,3, {name="hello", test="this"} ], [1,2,3, {test="this", name="hello"} ] );
99
}
100
101
function testisNotEqual() {
102
$assert.isNotEqual( this, new coldbox.system.MockBox() );
103
$assert.isNotEqual( "hello", "test" );
104
$assert.isNotEqual( 1, 2 );
105
$assert.isNotEqual( [], [1,3] );
106
}
107
108
function testisEqualWithCase() {
109
$assert.isEqualWithCase( "hello", "hello" );
110
}
111
112
function testnullValue() {
113
$assert.null( javaCast("null", "") );
114
}
115
116
function testNotNullValue() {
117
$assert.notNull( 44 );
118
}
119
120
function testTypeOf() {
121
$assert.typeOf( "array", [ 1,2 ] );
122
$assert.typeOf( "boolean", false );
123
$assert.typeOf( "component", this );
124
$assert.typeOf( "date", now() );
125
$assert.typeOf( "time", timeformat( now() ) );
126
$assert.typeOf( "float", 1.1 );
127
$assert.typeOf( "numeric", 1 );
128
$assert.typeOf( "query", querySim( "id, name
129
1 | luis") );
130
$assert.typeOf( "string", "hello string" );
131
$assert.typeOf( "struct", { name="luis", awesome=true } );
132
$assert.typeOf( "uuid", createUUID() );
133
$assert.typeOf( "url", "http://www.coldbox.org" );
134
}
135
136
function testNotTypeOf() {
137
$assert.notTypeOf( "array", 1 );
138
$assert.notTypeOf( "boolean", "hello" );
139
$assert.notTypeOf( "component", {} );
140
$assert.notTypeOf( "date", "monday" );
141
$assert.notTypeOf( "time", "1");
142
$assert.notTypeOf( "float", "Hello" );
143
$assert.notTypeOf( "numeric", "eeww2" );
144
$assert.notTypeOf( "query", [] );
145
$assert.notTypeOf( "string", this );
146
$assert.notTypeOf( "struct", [] );
147
$assert.notTypeOf( "uuid", "123" );
148
$assert.notTypeOf( "url", "coldbox" );
149
}
150
151
function testInstanceOf() {
152
$assert.instanceOf( new coldbox.system.MockBox(), "coldbox.system.MockBox" );
153
}
154
155
function testNotInstanceOf() {
156
$assert.notInstanceOf( this, "coldbox.system.MockBox" );
157
}
158
159
function testMatch(){
160
$assert.match( "This testing is my test", "(TEST)quot; );
161
}
162
163
function testMatchWithCase(){
164
$assert.match( "This testing is my test", "(test)quot; );
165
}
166
167
function testNotMatch(){
168
$assert.notMatch( "This testing is my test", "(hello)quot; );
169
}
170
171
function testKey(){
172
$assert.key( {name="luis", awesome=true}, "awesome" );
173
}
174
175
function testNotKey(){
176
$assert.notKey( {name="luis", awesome=true}, "test" );
177
}
178
179
function testDeepKey(){
180
$assert.deepKey( {name="luis", awesome=true, parent = { age=70 } }, "age" );
181
}
182
183
function testNotDeepKey(){
184
$assert.notDeepKey( {name="luis", awesome=true, parent = { age=70 } }, "luis" );
185
}
186
187
function testLengthOf(){
188
$assert.lengthOf( "heelo", 5 );
189
$assert.lengthOf( [1,2], 2 );
190
$assert.lengthOf( {name="luis"}, 1 );
191
$assert.lengthOf( querySim( "id, name
192
1 | luis"), 1 );
193
194
}
195
196
function testNotLengthOf(){
197
$assert.notLengthOf( "heelo", 3 );
198
$assert.notLengthOf( [1,2], 5 );
199
$assert.notLengthOf( {name="luis"}, 5 );
200
$assert.notLengthOf( querySim( "id, name
201
1 | luis"), 0 );
202
203
}
204
205
// railo 4.1+ or CF10+
206
function testThrows(){
207
$assert.throws(function(){
208
var hello = invalidFunction();
209
});
210
}
211
212
// railo 4.1+ or CF10+
213
function testNotThrows(){
214
$assert.notThrows(function(){
215
var hello = 1;
216
});
217
}
218
219
/*********************************** NON-RUNNABLE Methods ***********************************/
220
221
function nonStandardNamesWillNotRun() {
222
fail( "Non-test methods should not run" );
223
}
224
225
private function privateMethodsDontRun() {
226
fail( "Private method don't run" );
227
}
228
229
}
Copied!

Custom Assertions

You can also register custom assertions within the $assert object. You will do this by reading our Custom Assertions section of our TestBox docs.
Last modified 3yr ago