Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Api Laravel 6单元测试响应401_Api_Unit Testing_Testing_Laravel 6 - Fatal编程技术网

Api Laravel 6单元测试响应401

Api Laravel 6单元测试响应401,api,unit-testing,testing,laravel-6,Api,Unit Testing,Testing,Laravel 6,我正在使用基于令牌的操作。项目中的编辑操作请求在Postman中运行良好 public function update(Request $request, $id) { $project = Project::find($id); $project->project_name = request('project_name'); $project->project_description = request('project_d

我正在使用基于令牌的操作。项目中的编辑操作请求在Postman中运行良好

public function update(Request $request, $id)
    {
        $project = Project::find($id);
        $project->project_name = request('project_name');
        $project->project_description = request('project_description');
        $project->save();
        return response()->json([
            'message' =>  "Project Updated Successfully",
            'updatedId' => $project->id
            ], 200);
    }
但是在单元测试中没有得到正确的响应

 public function testBasicExample()
    {
        $response = $this->withHeaders([
            'Accept' => 'application/json',
            'Authorization' => 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOi......'
        ])->json('POST', '/api/updateProject/1',
                ['project_name' => 'Sally'],
                ['project_description' => 'testing the api']
                );

                $response->assertStatus(200);
                $response->assertJson(['status' => true]);
    }
错误显示

PS D:\XMAPP\htdocs\minidmsapi> ./vendor/bin/phpunit
PHPUnit 8.4.1 by Sebastian Bergmann and contributors.

..F.                                                                4 / 4 (100%)

Time: 516 ms, Memory: 18.00 MB

There was 1 failure:

1) Modules\Projects\Tests\Unit\ProjectTest::testupdate
Expected status code 200 but received 401.
Failed asserting that false is true.

D:\XMAPP\htdocs\minidmsapi\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestResponse.php:166
D:\XMAPP\htdocs\minidmsapi\Modules\Projects\Tests\Unit\ProjectTest.php:35

FAILURES!
Tests: 4, Assertions: 4, Failures: 1.
为什么单元测试有错误响应

任何人,请向我解释单元测试中的实际错误和解决方案。

你通过了

['project_description' => 'testing the api']
illumb\Foundation\Testing\Concerns\MakesHttpRequests::json()
调用的
$headers

public function json($method, $uri, array $data = [], array $headers = [])
这导致您的
授权
标题被覆盖

我想你的意思是把你的第三个和第四个参数组合作为第三个

$response = $this->withHeaders([
        'Accept' => 'application/json',
        'Authorization' => 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOi......'
    ])
    ->json(
        'POST', 
        '/api/updateProject/1',
        [
            'project_name' => 'Sally',
            'project_description' => 'testing the api'
        ]
    );

我用你的密码试过了。但是
$response->assertStatus(200)$响应->assertJson(['status'=>true])此代码会出现相同的错误!!!有什么解决办法吗?