Documentation
¶
Overview ¶
Package kittest tests Tern apps from Go: call a load, an action or an endpoint with a request made up for the test, and check what it returns; or run the whole app and query the HTML its pages render.
A load, as a signed-in user, against a database made for the test:
func TestProjects(t *testing.T) {
db := kittest.SQLite(t, migrations.FS)
acc := ... // accounts.New(db, …)
app := kittest.App(t, kit.WithPlugins(acc))
c := kittest.Ctx(t, app, kittest.Path("/projects"))
acc.ActAs(c, user)
data, err := projects.Load(c)
...
}
The whole app, in a test in package main, with its generated ternApp:
func TestHome(t *testing.T) {
app, err := ternApp()
...
client := kittest.NewClient(t, app)
page := client.Get("/")
if page.Status != 200 || page.Text("h1") != "Welcome" { ... }
}
Index ¶
- func App(t testing.TB, opts ...kit.Option) *kit.App
- func Ctx(t testing.TB, app *kit.App, opts ...Option) *kit.Ctx
- func Failed(err error) (status int, data any, ok bool)
- func Redirected(err error) (status int, location string, ok bool)
- func Response(c *kit.Ctx) *httptest.ResponseRecorder
- func SQLite(t testing.TB, migrations ...fs.FS) *sql.DB
- func Status(err error) int
- type Client
- type Element
- type Option
- func Context(ctx context.Context) Option
- func Cookie(c *http.Cookie) Option
- func Form(values url.Values) Option
- func Header(name, value string) Option
- func JSON(v any) Option
- func Local(key string, v any) Option
- func Method(m string) Option
- func NoHooks() Option
- func Parent(v any) Option
- func Path(p string) Option
- func Route(id string, params ...string) Option
- type Page
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func App ¶
App returns an App with no routes, for calling loads, actions and endpoints with Ctx. Pass the options the real app uses, its plugins among them, so their hooks run. It is closed when the test ends.
func Ctx ¶
Ctx returns the Ctx of a request made up of opts, with app's hooks run: what a load, action or endpoint gets. Its response is a recorder: Response returns it.
func Redirected ¶
Redirected reports whether err redirects, and where.
func Response ¶
func Response(c *kit.Ctx) *httptest.ResponseRecorder
Response is the response written to a Ctx from Ctx so far, with the headers and cookies it set.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client sends requests to an app in the test, as a browser would: it keeps cookies, sends forms from the app's own origin, and doesn't follow redirects, so tests see them.
func NewClient ¶
NewClient serves h, usually the App from the app's ternApp, for the test. An App is started first, so its plugins' start functions run, migrations among them, and is closed when the test ends.
type Element ¶
type Element struct {
// contains filtered or unexported fields
}
Element is an HTML element of a Page.
type Option ¶
type Option func(*request)
Option describes the request of a Ctx.
func Method ¶
Method sets the request's method. The default is GET, or POST with a form or JSON body.
type Page ¶
type Page struct {
*http.Response
Status int
Body string
// contains filtered or unexported fields
}
Page is a response, its body read, and its HTML parsed when it is HTML.
func (*Page) Find ¶
Find returns the elements matching a CSS selector. The selectors are a subset: tag names, #id, .class, [attr], [attr=value], combined (a.button[href]), with descendant (space) and child (>) combinators, and lists (h1, h2).