{"id":4761,"date":"2024-02-12T05:08:12","date_gmt":"2024-02-12T05:08:12","guid":{"rendered":"https:\/\/rengga.dev\/blog\/?p=4761"},"modified":"2024-02-12T05:08:12","modified_gmt":"2024-02-12T05:08:12","slug":"debug-methods-built-into-laravels-testresponse","status":"publish","type":"post","link":"https:\/\/rengga.dev\/blog\/debug-methods-built-into-laravels-testresponse\/","title":{"rendered":"Debug Methods Built Into Laravel\u2019s TestResponse"},"content":{"rendered":"<p>Looking at Laravel\u2019s\u00a0<a href=\"https:\/\/laravel.com\/docs\/10.x\/http-tests#debugging-responses\">HTTP Tests<\/a>\u00a0documentation carefully, you\u2019ll notice some useful helpers built into the\u00a0<code>TestResponse<\/code>\u00a0class to debug HTTP responses. This class recently had some internal updates to these methods in the way of the\u00a0<a href=\"https:\/\/laravel-news.com\/laravel-11-dumpable-trait\">Laravel 11&#8217;s Dumpable Trait<\/a>, and I thought it would be a good time to revisit these useful helpers:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$response = $this-&gt;get('\/');\r\n \r\n$response-&gt;dump();\r\n$response-&gt;dumpHeaders();\r\n$response-&gt;dumpSession();<\/pre>\n<p>These methods have a\u00a0<code>dd()<\/code>\u00a0counterpart too, which will \u201cdie and dump\u201d response values:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$response-&gt;dd();\r\n$response-&gt;ddHeaders();\r\n$response-&gt;ddSession();\r\n<\/pre>\n<p>These methods are helpful shortcuts to just doing this yourself in a test:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">dump($response-&gt;headers-&gt;all());\r\n\/\/ or\r\ndd($response-&gt;headers-&gt;all());<\/pre>\n<p>However, where these methods shine is when you modify an existing test that chains various assertions on the\u00a0<code>TestResponse<\/code>\u00a0instance:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$this-&gt;postJson(route('post.store', $post))\r\n    -&gt;assertSessionHasNoErrors()\r\n    -&gt;assertCreated()\r\n    -&gt;assertJson(['created' =&gt; true]);<\/pre>\n<p>Let\u2019s say that\u00a0<code>assertSessionHasNoErrors()<\/code> fails, and I want to debug. I\u2019ve noticed when I come across a chain that doesn\u2019t assign the response, that I assign the response to a local variable like so:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$response = $this-&gt;postJson(route('post.store', $post));\r\n \r\ndd($response);\r\n \r\n$response\r\n    -&gt;assertSessionHasNoErrors()\r\n    -&gt;assertCreated()\r\n    -&gt;assertJson(['created' =&gt; true]);<\/pre>\n<p>I will not argue whether you should chain assertions and never assign a local variable\u2014that is more of a style preference\u2014but you\u2019ll come across situations where no local variable is assigned. Using these debug methods, we can quickly debug right before a test failure:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$this-&gt;postJson(route('post.store', $post))\r\n+   -&gt;dumpSession()\r\n    -&gt;assertSessionHasNoErrors()\r\n    -&gt;assertCreated()\r\n    -&gt;assertJson(['created' =&gt; true]);<\/pre>\n<p>These debug helpers are handy when you want to debug the response somewhere between various assertions without grabbing the\u00a0<code>TestResponse<\/code>\u00a0instance in a local variable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Looking at Laravel\u2019s\u00a0HTTP Tests\u00a0documentation carefully, you\u2019ll notice some useful helpers built into <a class=\"read-more\" href=\"https:\/\/rengga.dev\/blog\/debug-methods-built-into-laravels-testresponse\/\" title=\"Debug Methods Built Into Laravel\u2019s TestResponse\" itemprop=\"url\"><\/a><\/p>\n","protected":false},"author":1,"featured_media":4762,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[708,726,676],"newstopic":[597,574],"class_list":{"0":"post-4761","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-web-development","8":"tag-laravel","9":"tag-laravels-testresponse","10":"tag-rengga-dev","11":"newstopic-laravel","12":"newstopic-php"},"_links":{"self":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/comments?post=4761"}],"version-history":[{"count":1,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4761\/revisions"}],"predecessor-version":[{"id":4763,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4761\/revisions\/4763"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media\/4762"}],"wp:attachment":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media?parent=4761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/categories?post=4761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/tags?post=4761"},{"taxonomy":"newstopic","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/newstopic?post=4761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}