Expected status code 200 but received 500

That’s the code for my unit test. It’s working fine on local machine but when I push it to gitLab, I get

Expected status code 200 but received 500.
Failed asserting that false is true.

I’ve other similar tests too, but those tests are working fine. The error I’m getting is against edit url.

<?php

namespace Tests\Unit;

use App\Models\ACL\Permission;
use App\Models\ACL\Role;
use App\Models\Benefit\EmployeeDependent;
use App\Models\Employee\Employee;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class EmployeeDependentPolicyTest extends TestCase
{
    use RefreshDatabase;

    public function setUp(): void
    {
        parent::setUp();
        $this->seed();
    }

    /**
     * Full Admin
     * @return mixed
     */
    public function createUserWithAdminRole()
    {
        $admin = factory(Employee::class)->create();
        $admin->assignRole('admin');
        return $admin;
    }

    /**
     *
     * A user with edit first and last name of dependents can create new dependents
     * @return mixed
     */
    public function createEmployeeRoleWithCreateDependentPermissions()
    {
        $employeeRole = factory(Role::class)->create(['type' => 'employee']);
        DB::table('role_permission_has_access_levels')->insert([
            ['role_id' => $employeeRole->id, 'permission_id' => (Permission::where('name', 'edit employeedependent first_name')->pluck('id')->first()), 'access_level_id' => 0],
            ['role_id' => $employeeRole->id, 'permission_id' => (Permission::where('name', 'edit employeedependent last_name')->pluck('id')->first()), 'access_level_id' => 0],
            ['role_id' => $employeeRole->id, 'permission_id' => (Permission::where('name', 'view employeedependent middlename')->pluck('id')->first()), 'access_level_id' => 0],
                ]);

        return $employeeRole;
    }

    /**
     * A user with any of the edit dependent permissions can edit dependent
     * @return mixed
     */
    public function createEmployeeRoleWithEditDependentPermissions()
    {
        $employeeRole = factory(Role::class)->create(['type' => 'employee']);
        DB::table('role_permission_has_access_levels')->insert([
            ['role_id' => $employeeRole->id, 'permission_id' => (Permission::where('name', 'view employeedependent first_name')->pluck('id')->first()), 'access_level_id' => 0],
            ['role_id' => $employeeRole->id, 'permission_id' => (Permission::where('name', 'edit employeedependent last_name')->pluck('id')->first()), 'access_level_id' => 0],
            ['role_id' => $employeeRole->id, 'permission_id' => (Permission::where('name', 'view employeedependent middlename')->pluck('id')->first()), 'access_level_id' => 0],
        ]);

        return $employeeRole;
    }

    /**
     * @return mixed
     */
    public function createEmployeeWithCreatePermissions()
    {
        $employee['employee'] = factory(Employee::class)->create();
        $employee['role'] = $this->createEmployeeRoleWithCreateDependentPermissions();
        $employee['employee']->assignRole($employee['role']->name);
        $employee['dependent']=$this->createEmployeeDependents($employee['employee']->id);
        return $employee;
    }
    /**
     * @return mixed
     */
    public function createEmployeeWithEditPermissions()
    {
        $employee['employee'] = factory(Employee::class)->create();
        $employee['role'] = $this->createEmployeeRoleWithEditDependentPermissions();
        $employee['employee']->assignRole($employee['role']->name);
        $employee['dependent']=$this->createEmployeeDependents($employee['employee']->id);
        return $employee;
    }

    /**
     *  Admin can view all employees' dependents
     */
    public function test_admin_can_view_dependents_of_all_employees()
    {
        $admin = $this->createUserWithAdminRole();
        $employee1 = $this->createEmployeeWithCreatePermissions();
        $response = $this->actingAs($admin)
            ->get('/en/employees/' . $employee1['employee']->id.'/dependents');
        $response->assertStatus(200);
    }

    /**
     * Admin can access create/edit dependents page for all Employees
     */
    public function test_admin_can_create_and_edit_dependents_for_all_employees()
    {
        $admin = $this->createUserWithAdminRole();
        $employee1 = $this->createEmployeeWithCreatePermissions();
        $response = $this->actingAs($admin)
            ->get('/en/employees/' . $employee1['employee']->id.'/dependents/create');
        $response->assertStatus(200);
        $response = $this->actingAs($admin)
            ->get('/en/employees/' . $employee1['employee']->id.'/dependents/'.$employee1['dependent']->id.'/edit');
        $response->assertStatus(200);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function createEmployeeDependents($id)
    {
        return factory(EmployeeDependent::class)->create(['emp_id'=> $id]);
    }

    /**
     *  Employee can view his dependents
     */
    public function test_employee_can_view_his_dependents()
    {
        $employee = $this->createEmployeeWithCreatePermissions();
        $response = $this->actingAs($employee['employee'])
            ->get('/en/employees/' . $employee['employee']->id.'/dependents');
        $response->assertStatus(200);
    }
    /**
     *  Employee who can create his dependents can also edit
     */
    public function test_employee_can_create_and_edit_his_dependents()
    {
        $employee = $this->createEmployeeWithCreatePermissions();
        $response = $this->actingAs($employee['employee'])
            ->get('/en/employees/' . $employee['employee']->id.'/dependents/create');
        $response->assertStatus(200);
        $response = $this->actingAs($employee['employee'])
            ->get('/en/employees/' . $employee['employee']->id.'/dependents/'.$employee['dependent']->id.'/edit');
        $response->assertStatus(200);
    }

    /**
     *  Employee who can no permission to edit first or last name cannot create his dependents but only edit them
     */
    public function test_employee_can_edit_his_dependents_but_not_create()
    {
        $employee = $this->createEmployeeWithEditPermissions();
        $response = $this->actingAs($employee['employee'])
            ->get('/en/employees/' . $employee['employee']->id.'/dependents/create');
        $response->assertStatus(403);
        $response = $this->actingAs($employee['employee'])
            ->get('/en/employees/' . $employee['employee']->id.'/dependents/'.$employee['dependent']->id.'/edit');
        $response->assertStatus(200);
    }
}

That’s my error log:

continuous-integration — test
00:23


latest: Pulling from jakzal/phpqa
Digest: sha256:9638cd6c39a865c5f80ae29d02348cce3918a6709d8f8cee4d9cb054860b81b1
Status: Image is up to date for jakzal/phpqa:latest
+ vendor/bin/phpunit --no-coverage --colors=never
PHPUnit 7.5.17 by Sebastian Bergmann and contributors.

R.R...RR....R............F.FF.............                        42 / 42 (100%)

Time: 21.39 seconds, Memory: 44.00 MB

There were 3 failures:

1) Tests\Unit\EmployeeDependentPolicyTest::test_admin_can_create_and_edit_dependents_for_all_employees
Expected status code 200 but received 500.
Failed asserting that false is true.

/drone/src/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:151
/drone/src/tests/Unit/EmployeeDependentPolicyTest.php:114

2) Tests\Unit\EmployeeDependentPolicyTest::test_employee_can_create_and_edit_his_dependents
Expected status code 200 but received 500.
Failed asserting that false is true.

/drone/src/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:151
/drone/src/tests/Unit/EmployeeDependentPolicyTest.php:147

3) Tests\Unit\EmployeeDependentPolicyTest::test_employee_can_edit_his_dependents_but_not_create
Expected status code 200 but received 500.
Failed asserting that false is true.

Leave a Comment