Courses

Testing in Laravel 11: Advanced Level

Debugging with dd() and dump()

Summary of this lesson:
- Use dd() and dump() methods for test debugging
- Inspect HTTP response content
- Dump headers and session information
- Understand different debugging techniques
- Improve test error investigation

I want to show you a few helper methods for debugging your failed HTTP tests.


Imagine the scenario in which you want to assert the Forbidden status. But instead of getting an error Expected response status code [403] but received 200., you receive a normal status 200. It could mean there's a Middleware problem or wrong permissions added.

But what is inside that 200, and how does that page actually look? How do you debug those internal parts?

In the test, you load the page and call assertForbidden(), so you don't see the actual response's content.

use function Pest\Laravel\actingAs;
 
beforeEach(function (): void {
$this->user = User::factory()->create();
});
 
test('guest cannot access products page', function () {
actingAs($this->user)
->get('/products/create')
->assertForbidden();
});

You can use the dd() method on the response itself to get the response.

use function Pest\Laravel\actingAs;
 
beforeEach(function (): void {
$this->user = User::factory()->create();
});
 
test('guest cannot access products page', function () {
actingAs($this->user)
->get('/products/create')
->dd()
->assertForbidden();
});

When you run the test, you get a complete response, including the whole HTML code. Then, you can inspect what happened and what the content was.

Also, if you don't want tests to stop running instead of a dd(), you can use the dump() method.

test('guest cannot access products page', function () {
actingAs($this->user)
->get('/products/create')
->dump()
->assertForbidden();
});

Other methods exist, for example, for dumping the headers dumpHeaders() or ddHeaders() and dumping the session dumpSession() or ddSession().

Previous: Too Many Failed Tests? Stop or Skip

No comments yet…

avatar
You can use Markdown