Smeltery
Get started

Quench (testing)

v0.1

Testing

rust
use smeltery::testing::TestApp;

async fn home() -> &'static str { "Hello" }

// In tests/http.rs, inside a #[test] function:
let app = TestApp::new(|app| app.routes(|r| { r.get("/", home); }));
let res = app.get("/");
assert_eq!(res.status(), 200);
assert_eq!(res.text(), "Hello");

TestApp::new(build) builds the whole app (all middleware, error pages and static files) with APP_ENV=testing; get, get_json, post_form, post_json, post_multipart and request send requests without a server (post_multipart(url, &[("title", "Sea")], &[TestFile::new("image", "sea.png", bytes)]); TestFile::with_mime sets the content type, and testing::multipart_body builds such a body for request). A generated app's tests/http.rs uses it with the app's own build. Like a browser, a TestApp keeps the cookies responses set and sends them with later requests, so sessions and flash messages work across requests; cookie(name), set_cookie(name, value) and clear_cookies() reach the jar. app.acting_as(user_id) signs a user in for the following requests, and .with_csrf() turns the CSRF check on. app.with_header(name, value) sends a header with every following request (a header given to request itself wins), app.without_header(name) stops it, and app.with_bearer(token) sends Authorization: Bearer <token>.

Each TestApp has its own database: TEST_DATABASE_URL when it is set, otherwise (with the sqlite feature) a fresh in-memory SQLite database, otherwise DATABASE_URL. Before TestApp::new returns it drops every table and runs every registered migration, so each test starts with an empty, migrated database. app.db() is the handle for creating data, and app.block_on(future) runs async calls:

rust
let app = TestApp::new(my_app::build);
let post = app.block_on(PostFactory.create(&app.db())).unwrap();
assert_eq!(app.get(&format!("/posts/{}", post.id)).status(), 200);

↑↓ moveEnter openEsc close