Laravel 9 运行 PHP Unit 测试时出现错误,报错为 “json not found”。

huangapple go评论63阅读模式
英文:

Laravel 9 error running PHP Unit tests fails with error json not found

问题

    public function test_index()
    {
        $response = $this->json('GET', '/api/distinct-kinds');
        $response->assertStatus(200);
    }
}
英文:

I have created the following unit test:

<?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);
    }
}

Then I run command: vendor/bin/phpunit --coverage-html tests/coverage I got the following errors:

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.

What should I use to fix this error?
Maybe I have to add use of some class to fix this error?
Could you advice me on how to fix it?

答案1

得分: 1

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

尝试使用发布测试中使用的TestCase:

use Tests\TestCase;
英文:

This type of testing is Release testing. Unit tests are for testing specific classes, not accessing complete routines.

Try using the TestCase used by Release tests:

use Tests\TestCase;

huangapple
  • 本文由 发表于 2023年2月24日 01:44:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定