Testing

Controller Tests

Test controller behavior through routes and focused constructors.

By Guillermo Alvarez - Published Updated

Prefer route-level controller tests

Most controller behavior is best tested through the application handler:

app := lazytest.New(t, appinit.App())
response := app.Get("/posts/missing")

response.AssertStatus(http.StatusNotFound)

This verifies routing, controller construction, action behavior, rendering, and error handling together.

Check rendered data

Assert the observable response:

response.AssertContains("Posts")

Avoid reaching into controller internals unless the behavior cannot be observed from the HTTP response.

Test constructors when wiring matters

Constructor tests are useful for dependency requirements:

_, err := posts.New(context.Background())
if err == nil {
    t.Fatal("expected missing posts service error")
}

Keep those tests focused on dependency resolution. Use HTTP tests for request behavior.