我正在嘗試使用 PHPUnit 9.0.0 和 Symfony 5.1.8 執行我的第一個單元測驗。如果 HTTP 回應為 200,則此測驗必須通過。
<?php declare(strict_types=1);
namespace Tests\Infrastructure\Api\Controller;
use PHPUnit\Framework\TestCase;
class ControllerTests extends TestCase
{
/** @test */
public function route(): void
{
$response = $this->get('/');
$response->assertStatus(200);
}
}
我得到錯誤:
有 1 個錯誤:
- Tests\Infrastructure\Api\Controller\SupplierControllerTests::route 錯誤:呼叫未定義的方法 Tests\Infrastructure\Api\Controller\SupplierControllerTests::get()
我認為 Get 方法是默認方法,由 PHPUnit\Framework\TestCase 匯入,但看起來不是。
我必須在我的類 ControllerTests 中添加一個 Get 方法嗎?我如何開發它來測驗 HTTP 回應?
提前致謝
uj5u.com熱心網友回復:
您需要擴展Symfony 提供的WebTestCase,而不是PHPUnit 提供的默認TestCase。
這是您嘗試撰寫的類似測驗的示例:
namespace App\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class PostControllerTest extends WebTestCase
{
public function testShowPost()
{
$client = static::createClient();
$client->request('GET', '/post/hello-world');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/401024.html
