php - 没有中间件的 Laravel 8 API 测试
问题描述
我正在尝试测试我的 Laravel API。我编写了一些简单的测试来发布和获取数据。如果我通过终端运行,这些测试按预期工作,php artisan test
但在使用名为 Husky 的包实现到 git 挂钩时无法正确运行。它在pre-push
test 命令上运行,并返回:
• 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
]);
}
}
解决方案
推荐阅读
- sql - PostgreSQL选择逗号分隔值列的不同值,不包括子集
- javascript - 为什么我的代码不起作用?
- python - 使用另一个列表中的值在数据框中添加行
- javascript - Photoshop 脚本 - 更改特定的文本图层内容
- vue.js - Vue emmitting 组件属性似乎跳过父级
- angular - 使变量在 AngularFire5 中可用
- azure-service-fabric - Azure Service Fabric IPv6 网络问题
- r - 将数据框写入 R 中的 xlsx 文件的问题
- c - 如果第二个参数完全包含在第一个参数中,它应该打印 true,但它打印 false
- java - OpenCL 编译失败