In this episode, we take a hands-on look at the new ddBody
method in Laravel's TestResponse
class, which makes it way easier to debug your application’s HTTP responses when writing tests. Whether you're using PHPUnit or Pest, this feature is accessible and super handy.
We start by writing a very basic test that hits a /user
endpoint and returns some JSON. Normally, if you want to see what's coming back in your response while writing tests, you might resort to a classic dd
(die and dump) on your response object, or maybe try to get the content and dump that. This works, but it’s kind of clunky, especially if you have to chain methods or extract certain bits of data.
Enter ddBody
. This method lets you instantly dump just the body of the response—whether it’s JSON or HTML—right in your test flow. Plus, if your response is JSON, you can pass a key to ddBody
and extract just the bit you’re interested in (like a user's name or ID), keeping your debug output clean.
We also touch on the other similar methods in the TestResponse
arsenal, like ddHeaders
, ddSession
, and ddJson
. These make it super convenient to peek into different parts of the HTTP response or session state while your tests are running.
The big benefit? You can sprinkle these dd
methods anywhere in your test chain, even if you're not explicitly saving the response to a variable. It's just a quick way to see what’s happening with your data, helping you write more accurate tests (and debug them a lot faster!).
Overall, ddBody
is a small but mighty addition to your toolkit when testing Laravel apps. Give it a go next time you get stuck figuring out what your endpoint is actually returning!