统一测试 laravel 和 php

发布于 2025-01-11 18:29:38 字数 8046 浏览 0 评论 0原文

这是我第一次在 PHP 和 Laravel 中进行统一测试,我遇到了这个问题:

Tests\Vocces\Company\Routes\CreateNewCompanyRouteTest > post create new company route
  Expected status code 201 but received 404.
  Failed asserting that 201 is identical to 404.

我认为我的问题在路上,但我不知道。我的 API 路由:

Route::post('/company', [App\Http\Controllers\Api\Company\PostCreateCompanyController::class, '__invoke']);
Route::resource('company', CompanyController::class);

我认为我对 /company 路由的测试调用以及对此函数的调用:

public function __invoke(CreateCompanyRequest $request, CompanyCreator $service)
    {
        DB::beginTransaction();
        try {
            $company = $service->handle(Str::uuid(), $request->name);
            DB::commit();
            return response($company, 201);
        } catch (\Throwable $error) {
            DB::rollback();
            throw $error;
        }
    }

我的测试:

class CreateNewCompanyRouteTest extends TestCase
{
    /**
     * @group route
     * @group access-interface
     * @test
     */
    public function postCreateNewCompanyRoute()
    {
        /**
         * Preparing
         */
        $faker = \Faker\Factory::create();
        $testCompany = [
            'name'   => $faker->name,
            'status' => 'inactive',
        ];

        /**
         * Actions
         */
        $response = $this->json('POST', '/api/company', [
            'name' => $testCompany['name'],
        ]);

        /**
         * Asserts
         */
        $response->assertStatus(201)
            ->assertJsonFragment($testCompany);
    }
}

我的应用程序有 9 个测试,我通过了 8 个测试,但我不知道它是什么是我的问题。我希望任何人都可以帮助我。

抱歉我的英语

更新:

| Domain | Method    | URI                      | Name              | Action                                                                 | Middleware |
+--------+-----------+--------------------------+-------------------+------------------------------------------------------------------------+------------+
|        | GET|HEAD  | /                        | home              | App\Http\Controllers\HomeController@index                              | web        |
|        | POST      | api/company              |                   | App\Http\Controllers\Api\Company\PostCreateCompanyController@__invoke  | api        |
|        | GET|HEAD  | api/company/index        | company.index     | App\Http\Controllers\Api\Company\CompanyController@index               | api        |
|        | GET|HEAD  | api/company/{id}/edit    | company.edit      | App\Http\Controllers\Api\Company\CompanyController@edit                | api        |
|        | PUT       | api/company/{id}/update  | company.update    | App\Http\Controllers\Api\Company\CompanyController@update              | api        |
|        | POST      | companies                | companies.store   | App\Http\Controllers\CompanyController@store                           | web        |
|        | GET|HEAD  | companies                | companies.index   | App\Http\Controllers\CompanyController@index                           | web        |
|        | GET|HEAD  | companies/create         | companies.create  | App\Http\Controllers\CompanyController@create                          | web        |
|        | DELETE    | companies/{company}      | companies.destroy | App\Http\Controllers\CompanyController@destroy                         | web        |
|        | PUT|PATCH | companies/{company}      | companies.update  | App\Http\Controllers\CompanyController@update                          | web        |
|        | GET|HEAD  | companies/{company}      | companies.show    | App\Http\Controllers\CompanyController@show                            | web        |
|        | GET|HEAD  | companies/{company}/edit | companies.edit    | App\Http\Controllers\CompanyController@edit                            | web        |
|        | GET|HEAD  | home                     | home              | App\Http\Controllers\HomeController@home                               | web        |
|        | GET|HEAD  | login                    | login             | App\Http\Controllers\Auth\LoginController@showLoginForm                | web        |
|        |           |                          |                   |                                                                        | guest      |
|        | POST      | login                    |                   | App\Http\Controllers\Auth\LoginController@login                        | web        |
|        |           |                          |                   |                                                                        | guest      |
|        | POST      | logout                   | logout            | App\Http\Controllers\Auth\LoginController@logout                       | web        |
|        | POST      | password/confirm         |                   | App\Http\Controllers\Auth\ConfirmPasswordController@confirm            | web        |
|        |           |                          |                   |                                                                        | auth       |
|        | GET|HEAD  | password/confirm         | password.confirm  | App\Http\Controllers\Auth\ConfirmPasswordController@showConfirmForm    | web        |
|        |           |                          |                   |                                                                        | auth       |
|        | POST      | password/email           | password.email    | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web        |
|        | POST      | password/reset           | password.update   | App\Http\Controllers\Auth\ResetPasswordController@reset                | web        |
|        | GET|HEAD  | password/reset           | password.request  | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web        |
|        | GET|HEAD  | password/reset/{token}   | password.reset    | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web        |
|        | POST      | register                 |                   | App\Http\Controllers\Auth\RegisterController@register                  | web        |
|        |           |                          |                   |                                                                        | guest      |
|        | GET|HEAD  | register                 | register          | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web        |
|        |           |                          |                   |                                                                        | guest      |
|        | GET|HEAD  | users                    | users.index       | App\Http\Controllers\UserController@index                              | web        |
|        | POST      | users                    | users.store       | App\Http\Controllers\UserController@store                              | web        |
|        | GET|HEAD  | users/create             | users.create      | App\Http\Controllers\UserController@create                             | web        |
|        | GET|HEAD  | users/{user}             | users.show        | App\Http\Controllers\UserController@show                               | web        |
|        | PUT|PATCH | users/{user}             | users.update      | App\Http\Controllers\UserController@update                             | web        |
|        | DELETE    | users/{user}             | users.destroy     | App\Http\Controllers\UserController@destroy                            | web        |
|        | GET|HEAD  | users/{user}/edit        | users.edit        | App\Http\Controllers\UserController@edit                               | web        |
+--------+-----------+--------------------------+-------------------+------------------------------------------------------------------------+------------+

it´s my first time that I´m doing the unitary test in PHP and laravel and I have this problem:

Tests\Vocces\Company\Routes\CreateNewCompanyRouteTest > post create new company route
  Expected status code 201 but received 404.
  Failed asserting that 201 is identical to 404.

i think that my problem it´s in route, but i don´t know. My API routes it´s:

Route::post('/company', [App\Http\Controllers\Api\Company\PostCreateCompanyController::class, '__invoke']);
Route::resource('company', CompanyController::class);

i think that my test call to /company route and this call to this function:

public function __invoke(CreateCompanyRequest $request, CompanyCreator $service)
    {
        DB::beginTransaction();
        try {
            $company = $service->handle(Str::uuid(), $request->name);
            DB::commit();
            return response($company, 201);
        } catch (\Throwable $error) {
            DB::rollback();
            throw $error;
        }
    }

my test:

class CreateNewCompanyRouteTest extends TestCase
{
    /**
     * @group route
     * @group access-interface
     * @test
     */
    public function postCreateNewCompanyRoute()
    {
        /**
         * Preparing
         */
        $faker = \Faker\Factory::create();
        $testCompany = [
            'name'   => $faker->name,
            'status' => 'inactive',
        ];

        /**
         * Actions
         */
        $response = $this->json('POST', '/api/company', [
            'name' => $testCompany['name'],
        ]);

        /**
         * Asserts
         */
        $response->assertStatus(201)
            ->assertJsonFragment($testCompany);
    }
}

my application has 9 tests I passed 8 but I don´t know what it´s my problem. I hope anybody can help me.

Sorry for my English

UPDATE:

| Domain | Method    | URI                      | Name              | Action                                                                 | Middleware |
+--------+-----------+--------------------------+-------------------+------------------------------------------------------------------------+------------+
|        | GET|HEAD  | /                        | home              | App\Http\Controllers\HomeController@index                              | web        |
|        | POST      | api/company              |                   | App\Http\Controllers\Api\Company\PostCreateCompanyController@__invoke  | api        |
|        | GET|HEAD  | api/company/index        | company.index     | App\Http\Controllers\Api\Company\CompanyController@index               | api        |
|        | GET|HEAD  | api/company/{id}/edit    | company.edit      | App\Http\Controllers\Api\Company\CompanyController@edit                | api        |
|        | PUT       | api/company/{id}/update  | company.update    | App\Http\Controllers\Api\Company\CompanyController@update              | api        |
|        | POST      | companies                | companies.store   | App\Http\Controllers\CompanyController@store                           | web        |
|        | GET|HEAD  | companies                | companies.index   | App\Http\Controllers\CompanyController@index                           | web        |
|        | GET|HEAD  | companies/create         | companies.create  | App\Http\Controllers\CompanyController@create                          | web        |
|        | DELETE    | companies/{company}      | companies.destroy | App\Http\Controllers\CompanyController@destroy                         | web        |
|        | PUT|PATCH | companies/{company}      | companies.update  | App\Http\Controllers\CompanyController@update                          | web        |
|        | GET|HEAD  | companies/{company}      | companies.show    | App\Http\Controllers\CompanyController@show                            | web        |
|        | GET|HEAD  | companies/{company}/edit | companies.edit    | App\Http\Controllers\CompanyController@edit                            | web        |
|        | GET|HEAD  | home                     | home              | App\Http\Controllers\HomeController@home                               | web        |
|        | GET|HEAD  | login                    | login             | App\Http\Controllers\Auth\LoginController@showLoginForm                | web        |
|        |           |                          |                   |                                                                        | guest      |
|        | POST      | login                    |                   | App\Http\Controllers\Auth\LoginController@login                        | web        |
|        |           |                          |                   |                                                                        | guest      |
|        | POST      | logout                   | logout            | App\Http\Controllers\Auth\LoginController@logout                       | web        |
|        | POST      | password/confirm         |                   | App\Http\Controllers\Auth\ConfirmPasswordController@confirm            | web        |
|        |           |                          |                   |                                                                        | auth       |
|        | GET|HEAD  | password/confirm         | password.confirm  | App\Http\Controllers\Auth\ConfirmPasswordController@showConfirmForm    | web        |
|        |           |                          |                   |                                                                        | auth       |
|        | POST      | password/email           | password.email    | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web        |
|        | POST      | password/reset           | password.update   | App\Http\Controllers\Auth\ResetPasswordController@reset                | web        |
|        | GET|HEAD  | password/reset           | password.request  | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web        |
|        | GET|HEAD  | password/reset/{token}   | password.reset    | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web        |
|        | POST      | register                 |                   | App\Http\Controllers\Auth\RegisterController@register                  | web        |
|        |           |                          |                   |                                                                        | guest      |
|        | GET|HEAD  | register                 | register          | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web        |
|        |           |                          |                   |                                                                        | guest      |
|        | GET|HEAD  | users                    | users.index       | App\Http\Controllers\UserController@index                              | web        |
|        | POST      | users                    | users.store       | App\Http\Controllers\UserController@store                              | web        |
|        | GET|HEAD  | users/create             | users.create      | App\Http\Controllers\UserController@create                             | web        |
|        | GET|HEAD  | users/{user}             | users.show        | App\Http\Controllers\UserController@show                               | web        |
|        | PUT|PATCH | users/{user}             | users.update      | App\Http\Controllers\UserController@update                             | web        |
|        | DELETE    | users/{user}             | users.destroy     | App\Http\Controllers\UserController@destroy                            | web        |
|        | GET|HEAD  | users/{user}/edit        | users.edit        | App\Http\Controllers\UserController@edit                               | web        |
+--------+-----------+--------------------------+-------------------+------------------------------------------------------------------------+------------+

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文