为什么Laravel Livewire使用PHPUnit测试时,API返回所有User记录时返回NotFoundHttpException?

wgx48brx  于 2023-06-07  发布在  PHP
关注(0)|答案(1)|浏览(116)

我在运行API测试时遇到了问题,该测试将返回用户模型中的所有记录。很简单目标是从这个模型中检索所有数据。它在浏览器、任何模式和Postman中都工作正常。它总是以JSON格式返回数据。很好。就这样。我用Livewire创建了测试和控制器。我使用的是最新版本的Laravel、Livewire等。我将与大家分享,控制器的代码,工作完美,以及测试代码,我的路由文件等首先,这是我的测试中出现的错误消息。
我已经改变了路由的顺序,我已经把路由放在routes/web.php文件中。我已经清除了config:clearcache等。我使用了指定的路由,我撤消了指定的路由。我已经在Postman和任何浏览器中测试过它。它可以工作。它不是一个单元测试。这是一个功能测试。它应该返回状态200,并且测试不应该失败并显示404错误。
请帮帮忙。

user@server:/var/www/virtual-card$ sudo php artisan test --filter ApiBuzCardTest::the_api_return_all_users

FAIL  Tests\Feature\Livewire\Api\ApiBuzCardTest
⨯ the api return all users                                                                                                                                                                                                                                  0.08s  
─────────────────────────────────────────────
FAILED  Tests\Feature\Livewire\Api\ApiBuzCardTest > the api return all users                                                                                                                                                              NotFoundHttpException  
GET https://server/virtual-card/api/users

at vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:51
47▕         $handler = $this->container->make(ExceptionHandler::class);
48▕
49▕         $handler->report($e);
50▕
➜  51▕         $response = $handler->render($passable, $e);
52▕
53▕         if (is_object($response) && method_exists($response, 'withException')) {
54▕             $response->withException($e);
55▕         }

      +42 vendor frames 

43  tests/Feature/Livewire/Api/ApiBuzCardTest.php:17

Tests:    1 failed (0 assertions)
Duration: 0.12s

我的控制器文件:app/Http/Livewire/Api/ApiBuzCard.php

<?php

namespace App\Http\Livewire\Api;

use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use Laravel\Jetstream\Features;
use Illuminate\Http\Request;
use Livewire\Component;

class ApiBuzCard extends Component
{
    public function getAllUsers()
    {
      $users = User::orderByDesc('id')->get()->toJson(JSON_PRETTY_PRINT);

      return response($users, 200);
    }

我的路由文件:routes/api.php

<?php

use App\Http\Livewire\Api\ApiBuzCard;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::get('users', [ApiBuzCard::class, 'getAllUsers'])->name('api.users');

Route::middleware('auth:sanctum')
    ->get('/user', function (Request $request) {
        return $request->user();
    });

Route::get('user/{id}', [ApiBuzCard::class, 'getUser']);

Route::post('users', [ApiBuzCard::class, 'createUser'])
    ->middleware('api');
Route::put('user/{id}', [ApiBuzCard::class, 'updateUser'])
    ->middleware('api');
Route::delete('user/{id}', [ApiBuzCard::class, 'deleteUser'])
    ->middleware('api');

我的测试文件tests/Feature/ApiBuzCardTest.php

<?php

namespace Tests\Feature\Livewire\Api;

use App\Http\Livewire\Api\ApiBuzCard;
use App\Models\User;
use Livewire\Livewire;
use Tests\TestCase;

class ApiBuzCardTest extends TestCase
{
    /** @test */
    public function the_api_return_all_users()
    {
        $this->withoutExceptionHandling();

        $user = User::factory()->create();

        $response = $this->get('/api/users');
        $response->assertStatus(200);
    }
5lwkijsr

5lwkijsr1#

我在下面的链接中找到了解决方案。用户Sergiu17建议问题是我的phpunit.xml文件中的以下配置:

<server name="APP_URL" value="http://devlab03.mshome.net/virtual-card"/>

虽然我在官方文档中找不到任何东西,但我相信Laravel测试环境只能在localhost上使用此配置。谢谢你。
https://laracasts.com/discuss/channels/code-review/laravel-feature-tests-always-return-404

相关问题