Summer Sale! All accounts are 50% off this week.

tenzan's avatar

assertOk() versus $this->withoutExceptionHandling();

Hi,

At 4:53 of the https://laracasts.com/series/pest-driven-laravel/episodes/10 , assertOk() is giving details error message, where in my case I see only:

  Expected response status code [200] but received 500.
Failed asserting that 500 is identical to 200.

or as follows if to show the whole output:

 ./vendor/bin/pest .\tests\Feature\PageCourseDetailsTest.php

   FAIL  Tests\Feature\PageCourseDetailsTest
  ⨯ it shows course details                                                                                                                                                          1.21s
  ⨯ it shows course video count                                                                                                                                                      0.74s
  ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   FAILED  Tests\Feature\PageCourseDetailsTest > it shows course details
  Expected response status code [200] but received 500.
Failed asserting that 500 is identical to 200.

  at tests\Feature\PageCourseDetailsTest.php:25
     21▕
     22▕     //act & assert
     23▕ //    $this->withoutExceptionHandling();
     24▕     get(route('course-details', $course))
  ➜  25▕         ->assertOk()
     26▕         ->assertSeeText([
     27▕             $course->title,
     28▕             $course->description,
     29▕             'Course tagline',

  ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   FAILED  Tests\Feature\PageCourseDetailsTest > it shows course video count
  Expected response status code [200] but received 500.
Failed asserting that 500 is identical to 200.

  at tests\Feature\PageCourseDetailsTest.php:45
     41▕     Video::factory()->count(3)->create(['course_id' => $course->id]);
     42▕
     43▕     //act & assert
     44▕     get(route('course-details', $course))
  ➜  45▕         ->assertOk()
     46▕         ->assertSeeText('3 videos');
     47▕ });
     48▕


  Tests:    2 failed (2 assertions)

Any idea about difference in error message between mine and in the video?

I was able to see the same error message as in video only when using $this->withoutExceptionHandling();.

0 likes
11 replies
LaryAI's avatar
Level 58

The difference in the error messages you're seeing is due to Laravel's exception handling. By default, Laravel's testing framework catches exceptions and converts them into HTTP responses with status codes. When an exception is thrown, Laravel will return a 500 status code, which is a generic server error response.

The assertOk() method is an assertion that checks if the response status code is 200. When your test receives a 500 status code, it means that an exception was thrown and not handled within your application, causing the test to fail with a simple status code mismatch message.

The $this->withoutExceptionHandling(); method disables Laravel's exception handling within the test, allowing exceptions to bubble up to the test framework. When you use this method, you get the actual exception message and stack trace, which can help you debug the issue more easily.

To see the detailed error message without disabling exception handling, you can check the Laravel log files, which by default are located in storage/logs/. Alternatively, you can temporarily use $this->withoutExceptionHandling(); in your test to get the detailed error message and stack trace directly in your test output.

Here's how you might modify your test to use $this->withoutExceptionHandling();:

/** @test */
public function it_shows_course_details()
{
    $this->withoutExceptionHandling(); // Temporarily disable exception handling

    $course = Course::factory()->create();

    // Your test code here...
    $response = $this->get(route('course-details', $course));

    $response->assertOk()
             ->assertSeeText([
                 $course->title,
                 $course->description,
                 // ... other text to assert
             ]);

    // Remember to remove or comment out the withoutExceptionHandling() call
    // once you've fixed the issue causing the exception.
}

Remember to remove or comment out the withoutExceptionHandling() call once you've fixed the issue causing the exception, as you'll want exceptions to be handled normally in most cases.

tisuchi's avatar

@tenzan Since you are getting 500 errors, which means you can see more details of the error in the laravel.log file.

Can you investigate the details there what exactly the error?

tenzan's avatar

@tisuchi Yeah, I know where the error log file. I'm just wondering why I and and video episode have different outputs even though both are using assertOK(). I'm using the latest Laravel 11.

martinbean's avatar

@tenzan I don’t think there is a problem or typo in your code. I’ve noticed in Laravel 11 (released only last week, so the video will be using an older version of Laravel) that I no longer get helpful information when a response assertion fails, which is causing no end of frustration. The cynic in me thinks this may have been semi-deliberate to try and get people to switch from PHPUnit to Pest.

1 like
tykus's avatar

Why do you think the problem is in the use of assertOk, and not an error/typo in your application code? I suppose if you actually shared the error message associated with the 500 response code???

tykus's avatar

@tenzan this is your code, not mine!

What does the logged error message say?

tenzan's avatar

@tykus Yes, I meant where's the typo in my code. The question is not about what's logged. The question is about why my output is different from the one in the video. Mine is short (see my original post), but in the video, it's detailed output like $this->withoutExceptionHandling(); used

tykus's avatar

@tenzan you assume the problem is in the test; I am saying it might be a problem in you application code.

Please or to participate in this conversation.