Skip to main content

ferro_test

Attribute Macro ferro_test 

Source
#[ferro_test]
Expand description

Attribute macro for database-enabled tests

This macro simplifies writing tests that need database access by automatically setting up an in-memory SQLite database with migrations applied.

By default, it uses crate::migrations::Migrator as the migrator type, following Ferro’s convention for migration location.

§Examples

use ferro::ferro_test;
use ferro::testing::TestDatabase;

#[ferro_test]
async fn test_user_creation(db: TestDatabase) {
    // db is an in-memory SQLite database with all migrations applied
    // Any code using DB::connection() will use this test database
    let action = CreateUserAction::new();
    let user = action.execute("test@example.com").await.unwrap();
    assert!(user.id > 0);
}

§Without TestDatabase parameter:

#[ferro_test]
async fn test_action_without_direct_db_access() {
    // Database is set up but not directly accessed
    // Actions using DB::connection() still work
    let action = MyAction::new();
    action.execute().await.unwrap();
}

§With custom migrator:

#[ferro_test(migrator = my_crate::CustomMigrator)]
async fn test_with_custom_migrator(db: TestDatabase) {
    // Uses custom migrator instead of default
}