|
| 1 | +# Unit & Integration Testing in Wheels |
| 2 | + |
| 3 | +## Two Test Frameworks |
| 4 | + |
| 5 | +Wheels has two test frameworks. **All new tests must use TestBox.** |
| 6 | + |
| 7 | +| | TestBox (current) | RocketUnit (legacy) | |
| 8 | +|---|---|---| |
| 9 | +| **Syntax** | `describe`/`it`/`expect` (BDD) | `test_methodName()` + `assert()` | |
| 10 | +| **Base class** | `wheels.WheelsTest` | `wheels.tests.Test` | |
| 11 | +| **Location** | `tests/specs/` | `vendor/wheels/tests/` | |
| 12 | +| **Runner URL** | `/wheels/app/tests` | `/wheels/tests/core` | |
| 13 | +| **Status** | Active, all new tests | Legacy, backwards-compat only | |
| 14 | + |
| 15 | +## TestBox Test Structure |
| 16 | + |
| 17 | +### File Layout |
| 18 | + |
| 19 | +``` |
| 20 | +tests/ |
| 21 | + _assets/ |
| 22 | + models/ <- Test-only model CFCs (not app models) |
| 23 | + Model.cfc <- Base model (extends wheels.Model) |
| 24 | + Author.cfc <- Test model with table() override |
| 25 | + Post.cfc |
| 26 | + specs/ |
| 27 | + models/ <- Model specs |
| 28 | + BatchProcessingSpec.cfc |
| 29 | + QueryBuilderSpec.cfc |
| 30 | + controllers/ <- Controller specs |
| 31 | + functional/ <- End-to-end specs |
| 32 | + populate.cfm <- Creates/seeds test tables (runs before every test suite) |
| 33 | + runner.cfm <- TestBox runner (web entry point) |
| 34 | +``` |
| 35 | + |
| 36 | +### Writing a Spec |
| 37 | + |
| 38 | +```cfm |
| 39 | +component extends="wheels.WheelsTest" { |
| 40 | + function run() { |
| 41 | + describe("Feature Name", () => { |
| 42 | +
|
| 43 | + it("does something specific", () => { |
| 44 | + var result = model("author").findAll(); |
| 45 | + expect(result.recordcount).toBeGT(0); |
| 46 | + }); |
| 47 | +
|
| 48 | + it("returns a model object", () => { |
| 49 | + var obj = model("author").findOne(order="id"); |
| 50 | + expect(IsObject(obj)).toBeTrue(); |
| 51 | + expect(obj.firstName).toBe("Per"); |
| 52 | + }); |
| 53 | +
|
| 54 | + }); |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +### Key Points |
| 60 | + |
| 61 | +- **Extend `wheels.WheelsTest`** — this injects all `application.wo` methods (like `model()`) into the test scope automatically. |
| 62 | +- **Use `function run()`** — TestBox calls this to discover specs. Not `init()`, not `config()`. |
| 63 | +- **Arrow functions work** — `() => {}` is fine for `describe`/`it`/`beforeEach`. |
| 64 | + |
| 65 | +## Test Models |
| 66 | + |
| 67 | +Test models live in `tests/_assets/models/` and extend the local `Model.cfc` (which extends `wheels.Model`). They use `table()` to map to test tables created by `populate.cfm`. |
| 68 | + |
| 69 | +```cfm |
| 70 | +// tests/_assets/models/Author.cfc |
| 71 | +component extends="Model" { |
| 72 | + function config() { |
| 73 | + table("c_o_r_e_authors"); |
| 74 | + hasMany("posts"); |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +The test environment sets `modelPath` to `tests/_assets/models/` so `model("author")` resolves to your test model, not an app model. |
| 80 | + |
| 81 | +## Test Data (populate.cfm) |
| 82 | + |
| 83 | +`tests/populate.cfm` runs before every test suite invocation. It creates tables and seeds data. |
| 84 | + |
| 85 | +**Always use DROP + CREATE, never IF NOT EXISTS:** |
| 86 | + |
| 87 | +```cfm |
| 88 | +<!--- DROP first — IF NOT EXISTS misses schema changes ---> |
| 89 | +<cftry> |
| 90 | + <cfquery datasource="#application.wheels.dataSourceName#"> |
| 91 | + DROP TABLE IF EXISTS c_o_r_e_posts |
| 92 | + </cfquery> |
| 93 | + <cfcatch></cfcatch> |
| 94 | +</cftry> |
| 95 | +
|
| 96 | +<!--- Then CREATE ---> |
| 97 | +<cfquery datasource="#application.wheels.dataSourceName#"> |
| 98 | +CREATE TABLE c_o_r_e_posts ( |
| 99 | + id #local.identityColumnType#, |
| 100 | + title varchar(250) NOT NULL, |
| 101 | + ... |
| 102 | + PRIMARY KEY(id) |
| 103 | +) #local.storageEngine# |
| 104 | +</cfquery> |
| 105 | +``` |
| 106 | + |
| 107 | +**Why not IF NOT EXISTS?** If you add a column (like `status`) to a table that already exists from a previous test run, IF NOT EXISTS skips the CREATE and the column is missing. DROP + CREATE guarantees a clean schema every time. |
| 108 | + |
| 109 | +## Running Tests |
| 110 | + |
| 111 | +### Via URL (most reliable) |
| 112 | + |
| 113 | +``` |
| 114 | +# All specs in a directory |
| 115 | +/wheels/app/tests?format=json&directory=tests.specs.models |
| 116 | +
|
| 117 | +# Single spec bundle |
| 118 | +/wheels/app/tests?format=json&bundles=tests.specs.models.BatchProcessingSpec |
| 119 | +
|
| 120 | +# HTML output (for browser) |
| 121 | +/wheels/app/tests?format=html&directory=tests.specs.models |
| 122 | +
|
| 123 | +# Force model cache reload (needed after adding new model CFCs) |
| 124 | +/wheels/app/tests?format=json&directory=tests.specs.models&reload=true |
| 125 | +``` |
| 126 | + |
| 127 | +### Via curl + node (for CLI parsing) |
| 128 | + |
| 129 | +```bash |
| 130 | +curl -sL "http://localhost:60006/wheels/app/tests?format=json&directory=tests.specs.models&reload=true" \ |
| 131 | + > /tmp/testbox_results.json && node -e " |
| 132 | +const j = JSON.parse(require('fs').readFileSync('/tmp/testbox_results.json', 'utf8')); |
| 133 | +console.log('Passed:', j.totalPass, '| Failed:', j.totalFail, '| Errors:', j.totalError); |
| 134 | +for (const b of j.bundleStats) { |
| 135 | + console.log('\n' + b.name + ' (' + b.totalPass + '/' + b.totalSpecs + ')'); |
| 136 | + function printSuite(s, indent) { |
| 137 | + for (const sp of (s.specStats || [])) { |
| 138 | + if (sp.status !== 'Passed') console.log(indent + '[FAIL] ' + sp.name + ': ' + sp.failMessage); |
| 139 | + } |
| 140 | + for (const ns of (s.suiteStats || [])) printSuite(ns, indent + ' '); |
| 141 | + } |
| 142 | + for (const s of b.suiteStats) printSuite(s, ' '); |
| 143 | +} |
| 144 | +" |
| 145 | +``` |
| 146 | + |
| 147 | +**Why node instead of jq?** The Wheels TestBox JSON response contains unquoted `true`/`false` booleans that break strict JSON parsers. Node's `JSON.parse` handles them. |
| 148 | + |
| 149 | +## Common Gotchas |
| 150 | + |
| 151 | +### 1. CFML Closure Scoping |
| 152 | + |
| 153 | +Closures in CFML have their own `local` scope. You **cannot** read/write outer `local` variables from inside a closure. |
| 154 | + |
| 155 | +```cfm |
| 156 | +// WRONG — `local.count` inside the closure is a DIFFERENT variable |
| 157 | +var count = 0; |
| 158 | +model("author").findEach(callback = function(author) { |
| 159 | + count++; // This modifies the closure's local.count, not the outer one |
| 160 | +}); |
| 161 | +expect(count).toBe(10); // FAILS — count is still 0 |
| 162 | +
|
| 163 | +// RIGHT — use a shared struct (structs are passed by reference) |
| 164 | +var result = {count: 0}; |
| 165 | +model("author").findEach(callback = function(author) { |
| 166 | + result.count++; // Modifies the shared struct |
| 167 | +}); |
| 168 | +expect(result.count).toBe(10); // PASSES |
| 169 | +``` |
| 170 | + |
| 171 | +### 2. Model Cache After Adding New CFCs |
| 172 | + |
| 173 | +After adding a new model CFC to `tests/_assets/models/`, the first test run may fail with errors like `table 'authorscopeds' not found` — Wheels is using default table name conventions because it hasn't loaded your `config()` yet. |
| 174 | + |
| 175 | +**Fix:** Add `&reload=true` to the test runner URL to clear the model cache. |
| 176 | + |
| 177 | +### 3. Table Naming in Test Models |
| 178 | + |
| 179 | +Always call `table()` in your test model's `config()` to map to the test table name. Without it, Wheels pluralizes the model name (e.g., `AuthorScoped` -> `authorscopeds`). |
| 180 | + |
| 181 | +```cfm |
| 182 | +component extends="Model" { |
| 183 | + function config() { |
| 184 | + table("c_o_r_e_authors"); // Explicit table name |
| 185 | + } |
| 186 | +} |
| 187 | +``` |
| 188 | + |
| 189 | +### 4. Drop Order for Foreign Keys |
| 190 | + |
| 191 | +Drop child tables before parent tables in `populate.cfm`: |
| 192 | + |
| 193 | +```cfm |
| 194 | +DROP TABLE IF EXISTS c_o_r_e_posts <!--- child (has authorid FK) ---> |
| 195 | +DROP TABLE IF EXISTS c_o_r_e_authors <!--- parent ---> |
| 196 | +``` |
| 197 | + |
| 198 | +### 5. Pre-existing Test Failures |
| 199 | + |
| 200 | +The `vendor/wheels/tests/` RocketUnit suite has some pre-existing failures (e.g., in `model.errors`). Don't chase these — they're known issues in the legacy suite. Focus on making your TestBox specs green. |
0 commit comments