The ship course is still the 0.x beta. A test helper that disappears belongs to the testing guide for your crate, not to a PHPUnit method of the same name.
#[laravel::test] on a function that takes Application sends the request through middleware, routing, and the handler. It does not bind a port. Nothing in the test should call 127.0.0.1:8000. Each test gets its own application, and Cargo runs tests in parallel.
src/testing.rs is what that application uses. for_testing sets APP_ENV to testing and applies the overrides in that file, including QUEUE_CONNECTION=sync, MAIL_MAILER=log, and refresh_database(). Sync queues run the job inside the test process. The log mailer writes the message instead of sending it. refresh_database gives the test a migrated database of its own. The structure guide says the starter tests need PostgreSQL for that refresh. Point DATABASE_URL at a database you can wipe.
A guest and a blank title
form submits like a browser. Validation failures redirect back. json sends Accept: application/json, so the same invalid body is a 422. assert_unprocessable is that status. assert_invalid checks the field for both the JSON body and session-flashed web errors.
use laravel::testing::prelude::*;
use app::models::User;
#[laravel::test]
async fn guests_cannot_store_notes(app: Application) {
app.post("/notes")
.form([("title", "")])
.await
.assert_redirect();
}
#[laravel::test]
async fn blank_titles_are_422_for_json(app: Application) -> laravel::Result<()> {
let user = User::factory().create().await?;
app.post("/notes")
.acting_as(&user)
.json(json!({ "title": "", "body": "A note" }))
.await
.assert_unprocessable()
.assert_invalid(&["title"]);
Ok(())
}
User::factory().create() is the models guide’s way to seed a user inside a test. acting_as is the same guide’s delete example. If the starter’s User has no factory yet, the models guide also shows create!(User { name: "Taylor", email: "taylor@example.com", password: "secret" }), which hashes the string for you. Use one of those two. Do not invent a third constructor.
The guest assertion is assert_redirect because Authenticate, or #[validate(authenticated)], sends a browser client to the login page. It does not assert the note was saved. Follow it with a test that signs in and checks the row:
#[laravel::test]
async fn authors_can_store_notes(app: Application) -> laravel::Result<()> {
let user = User::factory().create().await?;
app.post("/notes")
.acting_as(&user)
.from("/notes")
.form([
("title", "Read the migration"),
("body", "The table is in the binary."),
])
.await
.assert_redirect();
app.assert_database_has("notes", json!({
"user_id": user.id,
"title": "Read the migration",
})).await;
Ok(())
}
from sets the Referer header. assert_redirect_back_with_errors is the stricter check when you want the validation errors specifically. The testing guide uses it after from. Use it on the blank-title browser case if assert_redirect alone is too easy to satisfy with a login redirect.
Run a subset by name:
cargo test notes
cargo nextest run works too. A test with no Application argument does not boot the app. Keep those for pure functions. The note routes are not pure functions.
Check your work
cargo test notes fails before the store route exists, and passes once a guest is redirected and a blank JSON title returns 422.