1
votes

I am new to PHPUnit and TDD. I just upgrade my project from Laravel 5.4 to 5.5 with phpunit 6.5.5 installed . In the learning process, I wrote this test:

/** @test */
public function it_assigns_an_employee_to_a_group() {
    $group = factory(Group::class)->create();

    $employee = factory(Employee::class)->create();

    $this->post(route('employee.manage.group', $employee), [
        'groups' => [$group->id]
    ]);

    $this->assertEquals(1, $employee->groups);
}

And I have a defined route in the web.php file that look like this

Route::post('{employee}/manage/groups', 'ManageEmployeeController@group')
    ->name('employee.manage.group');

I have not yet created the ManageEmployeeController and when I run the test, instead of get an error telling me that the Controller does not exist, I get this error

Failed asserting that null matches expected 1.

How can I solve this issue please?

2
in the test you want to assert that of 1 equals to something, then it says it does not! it you have not assert for route existence, why do you expect that ? - Mahdi Younesi
I needed to use $this->withoutExceptionHandling() for the error to trigger. Your response helped me a lot to find the answer to my question. - Prince Lionel N'zi

2 Answers

2
votes

The exception was automatically handle by Laravel, so I disabled it using

$this->withoutExceptionHandling();

The test method now look like this:

/** @test */
public function it_assigns_an_employee_to_a_group() {

    //Disable exception handling
    $this->withoutExceptionHandling();

    $group = factory(Group::class)->create();

    $employee = factory(Employee::class)->create();

    $this->post(route('employee.manage.group', $employee), [
        'groups' => [$group->id]
    ]);

    $this->assertEquals(1, $employee->groups);
}
0
votes

You may not have create the method in the Controller but that doesn t mean your test will stop.
The test runs.It makes a call to your endpoint. It returns 404 status because no method in controller found.
And then you make an assertion which will fail since your post request wasn't successful and no groups were created for your employee.

Just add a status assertion $response->assertStatus(code) or
$response->assetSuccessful()