首页 > 解决方案 > 没有中间件的 Laravel 8 API 测试

问题描述

我正在尝试测试我的 Laravel API。我编写了一些简单的测试来发布和获取数据。如果我通过终端运行,这些测试按预期工作,php artisan test但在使用名为 Husky 的包实现到 git 挂钩时无法正确运行。它在pre-pushtest 命令上运行,并返回:

  • Tests\Feature\ApplicationsTest > retrieve application
  Expected status code 200 but received 422.
  Failed asserting that 200 is identical to 422.

我试过添加:

$this->withoutMiddleware();

这不起作用,我的测试文件中缺少什么......

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Tests\TestCase;

class ApplicationsTest extends TestCase
{
    use WithoutMiddleware;

    /**
     * Retrieve an application
     *
     * @return void
     */
    public function testRetrieveApplication()
    {

        $response = $this->withHeaders([
            'Content-Type' => 'application/json',
        ])->json('GET', '/api/application/some-id');

        $this->withoutMiddleware();
        $response->assertStatus(200)->assertJson([
          'success' => true
        ]);


    }

}

标签: phplaravelphpunit

解决方案


推荐阅读