Skip to main content
Copper applications are plain Go, so testing is plain go test. Copper helps in two ways: every package’s dependencies are explicit, so test doubles drop in naturally, and most Copper packages ship a <pkg>test sibling with ready-made test helpers.

Testing Handlers

Since handlers are exported methods on a router you construct yourself, you may unit-test them directly with httptest, without starting a server:
pkg/rockets/router_test.go
mux.SetURLVars (from github.com/gorilla/mux, Copper’s router) injects path parameters when you call a handler directly, since no routing happened. You may skip it for routes without {params}.
If you would like to exercise routing, middleware, and handlers together, build a real handler with chttp.NewHandler and serve it with httptest.NewServer.

Test Helpers

Most helpers live in a <pkg>test sibling package; a few (like clogger’s and cmetrics’s) ship in the package itself: To assert on your application’s logs, record them with clogger.NewRecorder:

Mocking Your Own Packages

You may follow the same convention for your own services. Define an interface in the package, and place a hand-written mock in a <pkg>test sibling so that other packages can use it:
pkg/rockets/rocketstest/svc.go
The var _ rockets.Svc = (*MockSvc)(nil) line asks the compiler to verify that the mock stays in sync with the interface, while exported fields like LaunchErr allow each test to force the failure it needs.