13-testing

command
v14.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Overview

Step 13 of 13: the whole service tested in memory.

main.go is step 12 unchanged; main_test.go is new. newApp(cfg, repo) takes its storage as a parameter, so a test builds the real application (real router, real middleware, real error map) around any repository it likes and httptest.New drives it without a port, a goroutine or a sleep. Every test constructs its own application and its own store, so nothing is shared and nothing has to be reset.

The test to internalize is the last one: failingRepo embeds the Repository interface and overrides a single method. One type, a few lines, and you have proven that a database outage renders as a generic 500 with the connection string kept server-side.

Learn: test an Iris application end to end in milliseconds, replace storage with a fake, and assert on status, JSON and headers with the httptest chain. Before: ../12-newbuilder

Run:

go test -v
ADMIN_PASSWORD=change-me go run main.go     # still a working server

Try:

go test -run TestInternalErrorsDoNotLeak -v        # PASS, and nothing about 10.0.0.3 in the output
go test -run TestBooksPaginationAndGuards -v       # PASS

Next: TUTORIAL.md in the Iris repository splits this file into packages (library, api, main) and ../../02-routing/routing-basics starts the topic sections. Every section of the README names the book chapter it matches.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL