Command Line Tools
wheels generate test
wheels generate test
Section titled “wheels generate test”Generate test files for models, controllers, views, and other components using TestBox BDD syntax.
Synopsis
Section titled “Synopsis”wheels generate test [type] [target] [options]wheels g test [type] [target] [options]CommandBox Parameter Syntax
Section titled “CommandBox Parameter Syntax”This command supports multiple parameter formats:
- Positional parameters:
wheels generate test model User(most common) - Named parameters:
type=value target=value(e.g.,type=model target=User) - Flag parameters:
--flagequalsflag=true(e.g.,--crudequalscrud=true)
Parameter Mixing Rules:
ALLOWED:
- All positional:
wheels generate test model User - All positional + flags:
wheels generate test model User --crud --factory - All named:
type=model target=User crud=true
NOT ALLOWED:
- Positional + named:
wheels generate test model target=User(causes error)
Recommendation: Use positional for type/target, flags for options: wheels generate test model User --crud --factory
Description
Section titled “Description”The wheels generate test command creates test files for various components of your Wheels application using TestBox 6 BDD syntax. All generated tests use standard CFML cfhttp() for HTTP testing and proper Wheels model() syntax, ensuring compatibility and reliability.
Arguments
Section titled “Arguments”| Argument | Description | Default |
|---|---|---|
type | Type of test: model, controller, view, unit, integration, api | Required |
target | Name of the component/object to test | Required |
Options
Section titled “Options”| Option | Description | Default |
|---|---|---|
--name | Name of the view (required for view tests) | "" |
--crud | Generate CRUD test methods (create, read, update, delete) | false |
--mock | Generate mock/stub examples (for unit tests) | false |
--factory | Generate factory examples using model().create() pattern | false |
--force | Overwrite existing files without prompting | false |
--open | Open the created file in default editor | false |
Test Types
Section titled “Test Types”The command generates different test structures based on the type:
| Type | Location | Purpose | Testing Method |
|---|---|---|---|
model | /tests/specs/models/ | Model validations, associations, callbacks, custom methods | Direct model instantiation |
controller | /tests/specs/controllers/ | Controller actions via HTTP requests | cfhttp() requests |
view | /tests/specs/views/ | View rendering via HTTP requests | cfhttp() requests |
unit | /tests/specs/unit/ | Service/library components with custom logic | Direct component instantiation |
integration | /tests/specs/integration/ | End-to-end workflow tests | cfhttp() requests |
api | /tests/specs/integration/api/ | API endpoints with JSON request/response | cfhttp() with JSON |
Examples
Section titled “Examples”Model Tests
Section titled “Model Tests”# Basic model testwheels generate test model UserOutput: tests/specs/models/UserSpec.cfc
Generated Code:
component extends="wheels.Testbox" {
function run() {
describe("User Model", function() {
beforeEach(function() { variables.user = model("User").new(); });
it("should validate required fields", function() { expect(user.valid()).toBe(false); // Add specific field validations here });
it("should have expected associations", function() { // Test your model associations here // Example: expect(isObject(user)).toBe(true); });
it("should test custom model methods", function() { // Test custom model methods here }); }); }}Model Test with CRUD Operations
Section titled “Model Test with CRUD Operations”Generate a model test with create, read, update, delete operations:
# With CRUD operationswheels generate test model Product --crudOutput: tests/specs/models/ProductSpec.cfc
Contains:
- Basic validation tests
it("should create a new product")- Testsmodel().new()andsave()it("should find an existing product")- TestsfindByKey()it("should update an existing product")- Tests property updates andsave()it("should delete a product")- Testsdelete()method
Sample CRUD Test:
it("should create a new product", function() { product.name = "Test Product"; expect(product.save()).toBe(true); var newProduct = product; expect(newProduct.id).toBeGT(0);});With factory pattern for test data
Section titled “With factory pattern for test data”wheels generate test model Order --crud --factoryOutput: tests/specs/models/UserSpec.cfc
Generated Code:
beforeEach(function() { // Factory pattern: create reusable test data with sensible defaults variables.order = model("Order").new({ // Add default test attributes here });});
it("should create a new order", function() { var newOrder = model("Order").create({ // Add test attributes }); expect(newOrder.id).toBeGT(0);});Key Features:
- Validation testing with
model().new()andvalid() - Association testing
- CRUD operations:
model().create(),findByKey(),save(),delete()(with--crud) - Factory pattern for reusable test data (with
--factory)
Controller Tests
Section titled “Controller Tests”# Basic controller testwheels generate test controller UsersOutput: tests/specs/controllers/UsersControllerSpec.cfc
Generated Code:
component extends="wheels.Testbox" {
function beforeAll() { variables.baseUrl = "http://localhost:8080"; }
function run() {
describe("Users Controller", function() {
it("should respond to index request", function() { cfhttp(url = "#variables.baseUrl#/users", method = "GET", result = "response"); expect(response.status_code).toBe(200); // Add more specific assertions for your controller actions }); }); }}With CRUD actions
Section titled “With CRUD actions”wheels generate test controller Products --crudOutput: tests/specs/controllers/UsersControllerSpec.cfc
Contains:
it("should list all products (index action)")- Tests GET/productsit("should display a specific product (show action)")- Tests GET/products/:idit("should create a new product (create action)")- Tests POST/productsit("should update an existing product (update action)")- Tests PUT/products/:idit("should delete a product")- Tests DELETE/products/:id
Sample Controller Test:
it("should list all products (index action)", function() { cfhttp(url = "#variables.baseUrl#/products", method = "GET", result = "response"); expect(response.status_code).toBe(200); expect(response.filecontent).toInclude("Products");});
it("should create a new product (create action)", function() { cfhttp(url = "#variables.baseUrl#/products", method = "POST", result = "response") { cfhttpparam(type = "formfield", name = "product[name]", value = "Test Product"); // Add more form fields as needed } expect(response.status_code).toBe(302); // Redirect on success});Key Features:
- HTTP-based testing using
cfhttp() - Tests for index, show, create, update, delete actions (with
--crud) - Response status code assertions
- Content verification
View Tests
Section titled “View Tests”# View rendering testwheels generate test view users editOutput: tests/specs/views/users/editViewSpec.cfc
Generated Code:
component extends="wheels.Testbox" {
function beforeAll() { variables.baseUrl = "http://localhost:8080"; }
function run() {
describe("Users edit View", function() {
it("should render edit view without errors", function() { // Test view rendering via HTTP request cfhttp(url = "#variables.baseUrl#/users/edit", method = "GET", result = "response"); expect(response.status_code).toBe(200); expect(response.filecontent).toInclude("Users"); });
it("should display required HTML elements", function() { cfhttp(url = "#variables.baseUrl#/users/edit", method = "GET", result = "response"); // Add specific HTML element assertions // expect(response.filecontent).toInclude("<form"); // expect(response.filecontent).toInclude("<input"); }); }); }}Key Features:
- HTTP-based rendering tests
- Status code verification
- HTML content assertions
Unit Tests
Section titled “Unit Tests”# Basic unit testwheels generate test unit OrderProcessorOutput: tests/specs/unit/OrderProcessorSpec.cfc
Generated Code:
component extends="wheels.Testbox" {
function run() {
describe("OrderProcessor Unit Tests", function() {
it("should test orderprocessor functionality", function() { // Create your service/component to test // var service = new app.lib.OrderProcessorService(); // Test your service methods here // expect(service.someMethod()).toBe(expectedValue); });
it("should handle edge cases", function() { // Test edge cases like empty strings, null values, etc. // expect(someFunction("")).toBe(expectedValue); });
it("should handle errors gracefully", function() { // Test error handling // expect(function() { // someFunction(invalidInput); // }).toThrow(); }); }); }}With mocking examples
Section titled “With mocking examples”wheels generate test unit PaymentService --mockOutput: tests/specs/unit/OrderProcessorSpec.cfc
Additional Mock Test:
it("should work with mocked dependencies", function() { // Example of using MockBox for mocking // var mockDependency = createMock("app.lib.DependencyService"); // mockDependency.$("someMethod").$results("mocked value"); // Test with mocked dependency});Key Features:
- Direct component instantiation
- Edge case testing
- Error handling tests
- MockBox examples (with
--mock)
Integration Tests
Section titled “Integration Tests”# End-to-end workflow testwheels generate test integration CheckoutFlow --crudOutput: tests/specs/integration/CheckoutFlowIntegrationSpec.cfc
Generated Code:
component extends="wheels.Testbox" { function beforeAll() { variables.baseUrl = "http://localhost:8080"; }
function run() {
describe("CheckoutFlow Integration Test", function() {
it("should complete the full checkoutflow workflow", function() { // Test complete user journey using HTTP requests
// 1. Visit listing page cfhttp(url = "#variables.baseUrl#/checkoutflows", method = "GET", result = "listResponse"); expect(listResponse.status_code).toBe(200);
// 2. Create new record cfhttp(url = "#variables.baseUrl#/checkoutflows", method = "POST", result = "createResponse") { cfhttpparam(type = "formfield", name = "checkoutflow[name]", value = "Integration Test"); } expect(createResponse.status_code).toBe(302); // Redirect on success
// 3. Verify listing shows new record cfhttp(url = "#variables.baseUrl#/checkoutflows", method = "GET", result = "verifyResponse"); expect(verifyResponse.filecontent).toInclude("Integration Test");
// 4. Add more workflow steps (update, delete, etc.) });
it("should complete operations within acceptable time", function() { var startTime = getTickCount(); cfhttp(url = "#variables.baseUrl#/checkoutflows", method = "GET", result = "response"); var endTime = getTickCount(); var executionTime = endTime - startTime; expect(executionTime).toBeLT(5000, "Request should complete in under 5 seconds"); }); }); }}Key Features:
- Multi-step workflow testing
- Complete user journey via HTTP
- Performance timing assertions
API Tests
Section titled “API Tests”# API endpoint tests with JSONwheels generate test api Users --crudOutput: tests/specs/integration/api/UsersAPISpec.cfc
Generated Code:
component extends="wheels.Testbox" {
function beforeAll() { variables.apiUrl = "http://localhost:8080/api"; }
function run() {
describe("Users API", function() {
it("should return paginated users via GET", function() { cfhttp(url = "#variables.apiUrl#/users", method = "GET", result = "response") { cfhttpparam(type = "header", name = "Accept", value = "application/json"); // Add authentication header if needed // cfhttpparam(type = "header", name = "Authorization", value = "Bearer TOKEN"); } expect(response.status_code).toBe(200); var jsonData = deserializeJSON(response.filecontent); expect(jsonData).toHaveKey("data"); expect(isArray(jsonData.data)).toBe(true); });
it("should create a new user via POST", function() { var postData = { name = "API Test User" }; cfhttp(url = "#variables.apiUrl#/users", method = "POST", result = "response") { cfhttpparam(type = "header", name = "Content-Type", value = "application/json"); cfhttpparam(type = "body", value = serializeJSON(postData)); } expect(response.status_code).toBe(201); var jsonData = deserializeJSON(response.filecontent); expect(jsonData.data).toHaveKey("id"); });
it("should return 401 for unauthorized requests", function() { // Test without authentication header cfhttp(url = "#variables.apiUrl#/users", method = "GET", result = "response"); // expect(response.status_code).toBe(401); // Add your authentication tests here }); }); }}Key Features:
- JSON request/response handling with
serializeJSON()anddeserializeJSON() - Content-Type and Accept headers
- Authentication testing placeholders
- RESTful status code assertions (200, 201, 401, etc.)
Additional Options
Section titled “Additional Options”# Force overwrite existing fileswheels generate test model User --force
# Generate and open in editorwheels generate test controller Products --crud --openGenerated Test Structure
Section titled “Generated Test Structure”All generated tests include:
- TestBox 6 BDD Syntax: Modern
describe()andit()syntax - Lifecycle Methods:
beforeAll(),beforeEach(),afterEach()hooks - Proper Testing Patterns:
- Models:
model().new(),model().create(),findByKey() - Controllers/Views/Integration:
cfhttp()with status code assertions - APIs: JSON serialization with proper headers
- Models:
- Helpful Placeholders: Comments guiding implementation
Running Tests
Section titled “Running Tests”# Run all testswheels test run
# Run specific test bundlewheels test run --testBundles=ProductSpec
# Run with coveragewheels test run --coverageBest Practices
Section titled “Best Practices”- Fill in Test Attributes: Replace
// Add test attributescomments with actual model attributes - Customize Assertions: Add specific assertions for your application’s business logic
- Use Factory Pattern: Use
--factoryflag for tests requiring multiple similar objects - Test Edge Cases: Add tests for empty values, null inputs, boundary conditions
- Clean Up Test Data: Use
afterEach()or transactions to clean up test data - Use Descriptive Test Names: Keep
it()descriptions clear and specific
Troubleshooting
Section titled “Troubleshooting”Tests Fail with “Model Not Found”
Section titled “Tests Fail with “Model Not Found””Ensure your model exists in /app/models/ before generating tests.
HTTP Tests Return 404
Section titled “HTTP Tests Return 404”Verify your routes are configured correctly in /config/routes.cfm.
Factory Tests Create Invalid Records
Section titled “Factory Tests Create Invalid Records”Add required attributes in the model().create() calls with valid test data.
See Also
Section titled “See Also”- wheels test run - Run tests
- Testing Guide - Testing documentation
- TestBox Documentation - TestBox framework docs