【问题标题】:Laravel 9 error running PHP Unit tests fails with error json not foundLaravel 9 错误运行 PHP 单元测试失败,错误 json not found
【发布时间】:2023-02-24 02:26:58
【问题描述】:

我创建了以下单元测试:

<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;

class DistrictKindTest extends TestCase
{
    /**
     * Index method unit test.
     *
     * @return void
     */
    public function test_index()
    {
        $response = $this->json('GET', '/api/distinct-kinds');
        $response->assertStatus(200);
    }
}

然后我运行命令:vendor/bin/phpunit --coverage-html tests/coverage 我得到以下错误:

PHPUnit 9.6.3 by Sebastian Bergmann and contributors.

E....                                                               5 / 5 (100%)

Time: 00:00.151, Memory: 20.00 MB

There was 1 error:

1) Tests\Unit\DistrictKindTest::test_index
Error: Call to undefined method Tests\Unit\DistrictKindTest::json()

/var/www/html/tests/Unit/DistrictKindTest.php:23

ERRORS!
Tests: 5, Assertions: 13, Errors: 1.

我应该用什么来修复这个错误? 也许我必须添加一些类的使用来修复这个错误? 你能建议我如何解决它吗?

【问题讨论】:

    标签: laravel api rest testing phpunit


    【解决方案1】:

    这种类型的测试是发布测试。单元测试用于测试特定的类,而不是访问完整的例程。

    尝试使用 Release 测试使用的 TestCase:

    use TestsTestCase;
    

    【讨论】:

      猜你喜欢
      • 2015-08-11
      • 1970-01-01
      • 2018-07-27
      • 2023-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多