Documentation
¶
Overview ¶
Package testdsl is a Given/When/Then DSL for testing your own use cases.
Import it from your tests when you want each test to read as a scenario:
func TestGreet(t *testing.T) {
testdsl.Test(t).
Given(Greet{}).
When(t.Context(), Hello{Name: "Gopher"}).
Then(Greeting{Text: "Hello, Gopher!"})
}
Given fixes the request and response types for the rest of the chain, so When only accepts the use case's request and Then only its response: a mismatched expectation does not compile. When.ThenFailWith expects an error instead, and When.ThenCheck runs a check of your own.
Like any assertion library, testdsl checks only what a scenario asks for: end each one with a Then method. It uses testify, which your tests then depend on too.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Given ¶
type Given[Req, Res any] struct { // contains filtered or unexported fields }
Given is a scenario with the use case under test.
type Scenario ¶
type Scenario struct {
// contains filtered or unexported fields
}
Scenario starts a test. Create one with Test.
type When ¶
type When[Res any] struct { // contains filtered or unexported fields }
When is a scenario whose use case has run, holding what it returned.
func (*When[Res]) Then ¶
func (w *When[Res]) Then(want Res)
Then expects the use case to have succeeded and returned want.
func (*When[Res]) ThenCheck ¶
ThenCheck runs check on what the use case returned, for expectations Then and ThenFailWith do not cover.
func (*When[Res]) ThenFailWith ¶
ThenFailWith expects the use case to have failed with an error matching want, as by errors.Is. A nil want never passes: the use case must have failed.