Test Model Configuration
setup()
Signature
Section titled “Signature”setup() — returns any
Available in: test
Category: Callback Functions
Description
Section titled “Description”Callback used in Wheels legacy testing framework. It runs before every individual test case within a test suite. This allows you to prepare the test environment, initialize objects, or reset state before each test executes.
Examples
Section titled “Examples”1. Basic setup for a test suite
component extends="app.tests.Test" {
function setup() {
// Initialize a new user object before each test
variables.user = model("user").new();
}
function test_User_Creation() {
variables.user.firstName = "John";
variables.user.lastName = "Doe";
assert("variables.user.save() eq true");
}
function test_User_Email_Validation() {
variables.user.email = "invalid-email";
assert("variables.user.valid() eq false");
}
}
2. Reset database table before each test
component extends="app.tests.Test" {
function setup() {
// Delete all records in the users table before each test
model("user").deleteAll();
}
function test_User_Insert() {
newUser = model("user").new(firstName="Alice", lastName="Smith");
assert("newUser.save() eq true");
}
function test_User_Count() {
count = model("user").count();
assert("count eq 0");
}
}