Skip to content

Commit

Permalink
Add tip for go-sqlmock and Close
Browse files Browse the repository at this point in the history
  • Loading branch information
System-Glitch committed Sep 19, 2024
1 parent f10bc02 commit 9f2c0b8
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/advanced/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,15 @@ func TestMockDB(t *testing.T) {
err = server.ReplaceDB(dialector)

//...

mock.ExpectClose()
}
```

:::tip
Test servers automatically close the database in a test cleanup hook. If you are using `go-sqlmock`, this will generate an error for unexpected `Close` unless you add `mock.ExpectClose()` at the very end of your test.
:::

## HTTP Tests

You may want to write tests that simulate how a client would interact with your API through HTTP calls. In order to do so, use the test server's `TestRequest(*http.Request)` method. It will execute the request all the way from the router's `ServeHTTP()` implementation. Therefore, the request's lifecycle will be executed from start to finish.
Expand Down

0 comments on commit 9f2c0b8

Please sign in to comment.