I'd like to perform a test with a simple description of membership registration using phpunit of laravel
A 500 error is returned without returning the expected status code.

I don't understand the cause.
Any advice can be very helpful.

Error message
│ Expected status code 200 but received 500.
   │ Failed asserting that false is true.</Code></pre>
<strong>Test code</strong>
<pre><code data-language = "PHP"><? php
namespace Tests \ Feature;
use App \ User;
use Tests \ TestCase;
use Illuminate \ Foundation \ Testing \ WithFaker;
use Illuminate \ Foundation \ Testing \ RefreshDatabase;
class RegisterApiTest extends TestCase
    use RefreshDatabase;
    / **
     * A basic test example.
     * @return void
     * /
    public function testRegister ()
        $data = [
            'name' =>'testUser',
            'email' =>'[email protected]',
            'password' =>'test1234',
            'password_confirmation' =>'test1234'
        $response = $this->json ('POST', route ('register'), $data);

        $user = User :: first ();
        $this->assertEquals ($data ['name'], $user->name);
        $response->assertStatus (201)->assertJson (['name' =>$user->name]);
Route definition (api.php)
Route :: post ('/ register', 'Auth \ RegisterController @ register')->name ('register');
Aut \ RegisterController.php
// Omitted above
// Add below
    protected function registered (Request $request, $user)
        return $user;

When I looked at the value of $response with dd (), it was already 500 around the first $this->json.

Supplemental information (FW/tool version etc.)

Please provide more detailed information here.
laravel 5.7.19

  • Answer # 1

    When testing using$this->json (),$this->get (), etc., usually test including exception handling Therefore, even if an unexpected exception occurs due to a bug, the default exception handler will normally handle and return status 500. If you want to investigate the cause, in this case, something should be output to the error log, so look at it. The error log should be in thestorage/logsdirectory if you haven't tampered with the settings yourself.

    If you do not test exception handling, if you suppress exception handling in advance with$this->withoutExceptionHandling (), an error will appear as it is in the output of phpunit This saves you the trouble of opening the error log.




    get ('/ foo');
            $response->assertOk ();
    If you prepare

    and run phpunit,

    % phpunit tests/Feature/FooTest.php
    PHPUnit 8.4.2 by Sebastian Bergmann and contributors.
    F 1/1 (100%)
    Time: 446 ms, Memory: 28.00 MB
    There was 1 failure:
    1) Tests \ Feature \ FooTest :: testFoo
    Response status code [500] does not match expected 200 status code.
    Failed asserting that false is true.
    Tests: 1, Assertions: 1, Failures: 1.

    and you can see that an error has occurred, but the cause is not clear from the output of phpunit.

    If you look at the error log, the cause of the error is output

    % cat storage/logs/laravel-2019-11-27.log
    [2019-11-27 05:38:21] testing.ERROR: Unexpected error {"exception": "[object] (Exception (code: 0): Unexpected error at /Users/matsui/src.nobk/laravel60 /app/Http/Controllers/FooController.php:13)
    # 0 [internal function]: App \\ Http \\ Controllers \\ FooController->foo ()
    # 1 /Users/matsui/src.nobk/laravel60/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array (Array, Array)
    # 2 /Users/matsui/src.nobk/laravel60/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate \\ Routing \\ Controller->callAction ('foo', Array)
    ... (omitted)

    $this->withoutExceptionHandling ()and add

    withoutExceptionHandling ();
            $response = $this->get ('/ foo');
            $response->assertOk ();

    The error will be written as it is in the output of phpunit.

    % phpunit tests/Feature/FooTest.php
    PHPUnit 8.4.2 by Sebastian Bergmann and contributors.
    E 1/1 (100%)
    Time: 766 ms, Memory: 24.00 MB
    There was 1 error:
    1) Tests \ Feature \ FooTest :: testFoo
    Exception: Unexpected error
    ... (omitted)
    Tests: 1, Assertions: 0, Errors: 1.

Related articles