1

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
  • 1
    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 ? Commented Jan 29, 2018 at 11:38
  • I needed to use $this->withoutExceptionHandling() for the error to trigger. Your response helped me a lot to find the answer to my question. Commented Jan 29, 2018 at 11:57

2 Answers 2

2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

0

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.