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

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

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

问题

  1. public function test_index()
  2. {
  3. $response = $this->json('GET', '/api/distinct-kinds');
  4. $response->assertStatus(200);
  5. }
  6. }
英文:

I have created the following unit test:

  1. <?php
  2. namespace Tests\Unit;
  3. use PHPUnit\Framework\TestCase;
  4. class DistrictKindTest extends TestCase
  5. {
  6. /**
  7. * Index method unit test.
  8. *
  9. * @return void
  10. */
  11. public function test_index()
  12. {
  13. $response = $this->json('GET', '/api/distinct-kinds');
  14. $response->assertStatus(200);
  15. }
  16. }

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

  1. PHPUnit 9.6.3 by Sebastian Bergmann and contributors.
  2. E.... 5 / 5 (100%)
  3. Time: 00:00.151, Memory: 20.00 MB
  4. There was 1 error:
  5. 1) Tests\Unit\DistrictKindTest::test_index
  6. Error: Call to undefined method Tests\Unit\DistrictKindTest::json()
  7. /var/www/html/tests/Unit/DistrictKindTest.php:23
  8. ERRORS!
  9. 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:

  1. 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:

  1. 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:

确定