All pages
Powered by GitBook
1 of 1

wheels generate test

Generate test files for models, controllers, views, and other components.

Synopsis

Description

The wheels generate test command creates test files for various components of your Wheels application. It generates appropriate test scaffolding based on the component type and includes common test cases to get you started.

Arguments

Options

Examples

Model Test

Generates /tests/models/ProductTest.cfc:

Controller Test

Generates /tests/controllers/ProductsTest.cfc:

View Test

Generates a test for the products/index view.

CRUD Tests

Generates complete CRUD test methods for the controller.

View Test

Generates /tests/views/products/IndexTest.cfc:

Integration Test

Generates additional integration tests:

Test Types

Model Tests

Focus on:

  • Validations

  • Associations

  • Callbacks

  • Scopes

  • Custom methods

  • Data integrity

Controller Tests

Focus on:

  • Action responses

  • Parameter handling

  • Authentication/authorization

  • Flash messages

  • Redirects

  • Error handling

View Tests

Focus on:

  • Content rendering

  • Data display

  • HTML structure

  • Escaping/security

  • Conditional display

  • Helpers usage

Helper Tests

Route Tests

Test Fixtures

Generate Fixtures

Creates /tests/fixtures/products.cfc:

Test Helpers

Custom Assertions

Test Data Builders

Running Tests

Run all tests

Run specific test file

Run specific test method

Run with coverage

wheels test --coverage

Best Practices

  1. Test in isolation: Each test should be independent

  2. Use descriptive names: Test names should explain what they test

  3. Follow AAA pattern: Arrange, Act, Assert

  4. Clean up data: Use setup/teardown or transactions

  5. Test edge cases: Empty data, nulls, extremes

  6. Mock external services: Don't rely on external APIs

  7. Keep tests fast: Optimize slow tests

  8. Test one thing: Each test should verify one behavior

  9. Use fixtures wisely: Share common test data

  10. Run tests frequently: Before commits and in CI

Common Testing Patterns

Testing Private Methods

function test_private_method_through_public_interface() {
    // Don't test private methods directly
    // Test them through public methods that use them
    product = model("Product").new(name: "  Test  ");
    product.save(); // Calls private sanitize method
    assert(product.name == "Test");
}

Testing Time-Dependent Code

function test_expiration_date() {
    // Use specific dates instead of Now()
    testDate = CreateDate(2024, 1, 1);
    product = model("Product").new(
        expiresAt: DateAdd("d", 30, testDate)
    );
    
    // Test with mocked current date
    request.currentDate = testDate;
    assert(!product.isExpired());
    
    request.currentDate = DateAdd("d", 31, testDate);
    assert(product.isExpired());
}

Testing Randomness

function test_random_discount() {
    // Test the range, not specific values
    product = model("Product").new(price: 100);
    
    for (i = 1; i <= 100; i++) {
        discount = product.getRandomDiscount();
        assert(discount >= 0.05 && discount <= 0.25, 
               "Discount should be between 5% and 25%");
    }
}

See Also

  • wheels test run - Run tests

  • wheels test coverage - Test coverage

  • Testing Guide - Testing documentation