Forge
1.x
Docs/Forge/Testing
Open

Deprecated

Use Grove instead.

Reading1 min
Updated31 Jul 2026
Sourcev1/extensions/database/testing.mdx

Test DB Factory01

Use NewTestDB(t, opts...) for fast, isolated tests.

Default behavior:

  • in-memory sqlite

  • automatic cleanup via t.Cleanup

Options:

  • WithInMemory()

  • WithLogging()

  • WithAutoMigrate(models...)

  • WithTransactionRollback()

Example02

func TestUserRepo(t *testing.T) {
    db := database.NewTestDB(t,
        database.WithAutoMigrate(&User{}),
    )

    repo := database.NewRepository[User](db)
    ctx := context.Background()

    err := repo.Create(ctx, &User{Name: "alice", Email: "[email protected]"})
    require.NoError(t, err)
}

Fixture Helpers03

  • SeedFixtures(t, db, fixtures...)

  • TruncateTables(t, db, models...)

  • LoadFixture[T](t, db, id)

  • LoadAllFixtures[T](t, db)

Assertion Helpers04

  • AssertRecordExists[T]

  • AssertRecordNotExists[T]

  • AssertRecordCount[T]

  • AssertRecordCountWhere[T]

These helpers reduce repetitive boilerplate in repository and service tests.