Ожидаемый код состояния 200, но получил 500 - PullRequest
2 голосов
/ 01 апреля 2020

Это код моего модульного теста. Он работает нормально на локальной машине, но когда я отправляю sh его на gitLab, я получаю

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

У меня есть и другие подобные тесты, но эти тесты работают нормально. Я получаю ошибку против 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);
    }
}

Это мой журнал ошибок:

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.

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

--

There were 5 risky tests:

1) Tests\Feature\EditMangerTest::test_admin_can_edit_manager
This test did not perform any assertions

/drone/src/tests/Feature/EditMangerTest.php:14

2) Tests\Feature\createRoleTest::test_user_can_create_role
This test did not perform any assertions

/drone/src/tests/Feature/createRoleTest.php:11

3) Tests\Unit\Approval\restoreStandardApprovalTest::testStandardApprovalHaveDefaultFields
This test did not perform any assertions

/drone/src/tests/Unit/Approval/restoreStandardApprovalTest.php:14

4) Tests\Unit\Approval\restoreStandardApprovalTest::testRestoreStandardApproval
This test did not perform any assertions

/drone/src/tests/Unit/Approval/restoreStandardApprovalTest.php:20

5) Tests\Unit\DefaultApprovalsTest::testDefaultApprovalsCount
This test did not perform any assertions

/drone/src/tests/Unit/DefaultApprovalsTest.php:22

FAILURES!
Tests: 42, Assertions: 100, Failures: 3, Risky: 5.
DocumentationPluginsSupport
GitHubTwitterDiscourseGitter
...