v0.22.0 Latest Latest

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

Go to latest
Published: Feb 26, 2024 License: MIT Imports: 20 Imported by: 0



Package tests provides common helpers and mocks used in PocketBase application tests.



This section is empty.


This section is empty.


func MockLogsData added in v0.20.0

func MockLogsData(app *TestApp) error

func MockMultipartData

func MockMultipartData(data map[string]string, fileFields ...string) (*bytes.Buffer, *multipart.Writer, error)

MockMultipartData creates a mocked multipart/form-data payload.


data, mp, err := tests.MockMultipartData(
	map[string]string{"title": "new"},

func TempDirClone added in v0.7.0

func TempDirClone(dirToClone string) (string, error)

TempDirClone creates a new temporary directory copy from the provided directory path.

It is the caller's responsibility to call `os.RemoveAll(tempDir)` when the directory is no longer needed!


type ApiScenario

type ApiScenario struct {
	Name           string
	Method         string
	Url            string
	Body           io.Reader
	RequestHeaders map[string]string

	// Delay adds a delay before checking the expectations usually
	// to ensure that all fired non-awaited go routines have finished
	Delay time.Duration

	// Timeout specifies how long to wait before cancelling the request context.
	// A zero or negative value means that there will be no timeout.
	Timeout time.Duration

	// expectations
	// ---
	ExpectedStatus     int
	ExpectedContent    []string
	NotExpectedContent []string
	ExpectedEvents     map[string]int

	// test hooks
	// ---
	TestAppFactory func(t *testing.T) *TestApp
	BeforeTestFunc func(t *testing.T, app *TestApp, e *echo.Echo)
	AfterTestFunc  func(t *testing.T, app *TestApp, res *http.Response)

ApiScenario defines a single api request test case/scenario.

func (*ApiScenario) Test

func (scenario *ApiScenario) Test(t *testing.T)

Test executes the test scenario.

type TestApp

type TestApp struct {

	// EventCalls defines a map to inspect which app events
	// (and how many times) were triggered.
	// The following events are not counted because they execute always:
	// - OnBeforeBootstrap
	// - OnAfterBootstrap
	// - OnBeforeServe
	EventCalls map[string]int

	TestMailer *TestMailer
	// contains filtered or unexported fields

TestApp is a wrapper app instance used for testing.

func NewTestApp

func NewTestApp(optTestDataDir ...string) (*TestApp, error)

NewTestApp creates and initializes a test application instance.

It is the caller's responsibility to call `app.Cleanup()` when the app is no longer needed.

func (*TestApp) Cleanup

func (t *TestApp) Cleanup()

Cleanup resets the test application state and removes the test app's dataDir from the filesystem.

After this call, the app instance shouldn't be used anymore.

func (*TestApp) NewMailClient

func (t *TestApp) NewMailClient() mailer.Mailer

NewMailClient initializes (if not already) a test app mail client.

func (*TestApp) ResetEventCalls

func (t *TestApp) ResetEventCalls()

ResetEventCalls resets the EventCalls counter.

type TestMailer

type TestMailer struct {
	TotalSend   int
	LastMessage mailer.Message

	// @todo consider deprecating the above 2 fields?
	SentMessages []mailer.Message

TestMailer is a mock `mailer.Mailer` implementation.

func (*TestMailer) Reset

func (tm *TestMailer) Reset()

Reset clears any previously test collected data.

func (*TestMailer) Send

func (tm *TestMailer) Send(m *mailer.Message) error

Send implements `mailer.Mailer` interface.

Jump to

Keyboard shortcuts

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