testdsl

package
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: MIT Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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.

func (*Given[Req, Res]) When

func (g *Given[Req, Res]) When(ctx context.Context, req Req) *When[Res]

When runs the use case with ctx and req. Call the Then methods from the test's goroutine, as for any test that can fail.

type Scenario

type Scenario struct {
	// contains filtered or unexported fields
}

Scenario starts a test. Create one with Test.

func Test

func Test(tb testing.TB) *Scenario

Test starts a scenario reported to tb.

func (*Scenario) Given

func (s *Scenario) Given[Req, Res any](useCase interactor.UseCase[Req, Res]) *Given[Req, Res]

Given sets the use case under 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

func (w *When[Res]) ThenCheck(check func(tb testing.TB, got Res, err error))

ThenCheck runs check on what the use case returned, for expectations Then and ThenFailWith do not cover.

func (*When[Res]) ThenFailWith

func (w *When[Res]) ThenFailWith(want error)

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.

Jump to

Keyboard shortcuts

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