Testing and Console
Request tests call your router and assert on the response, with a fresh in-memory database per test.
Request tests
A *_test.px module exports
test_* functions, which polar test runs.
Leave their signatures off: the router's effects are inferred, so a
new resource doesn't break old tests.
module PostsControllerTest
uses
Std.{Assert, Option}
Routes
Schema
Ticket.Test
hosts
Node
functions
test_create_redirects_with_a_notice() {
Test.with_db(Schema.migrations, function() {
let reply = Test.post(Routes.router, Test.client(), "/posts", [{ name: "post[title]", value: "Hello" }])
Test.assert_redirect(reply, "/posts/1")
Assert.equal(Test.flash(reply, "notice"), Some("Post was created."))
})
}
exports
test_create_redirects_with_a_notice
Test.client() starts with a session, so
Test.post, patch and
delete carry the CSRF token;
post_without_token tests a rejection. Cookies a reply
sets travel with reply.client, and
Test.follow follows a redirect. Assertions:
assert_status, assert_redirect,
assert_body_contains,
assert_body_not_contains, assert_header.
with_db also starts the clock at 2026-01-01 and makes
Random count, so cookies and tokens are the same on
every run.
ticket test
ticket test [filter] runs polar test with
TICKET_ENV=test and
TICKET_DATABASE=:memory:. A filter runs the tests whose
name contains it.
ticket console
ticket console [-e EXPR] [--sandbox] opens
polar repl with the database connected through
Main.boot, and prints which database it is.
--sandbox uses a throwaway
:memory: database with the migrations applied.
Note: polar repl can't
import modules that live in folders yet (polar-lang P14), so the
console doesn't start in an app with
src/controllers/ until it can.