英文:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论