在 PHPUnit 中调用未定义的方法 decodeResponseJson()
Call to undefined method decodeResponseJson() in PHPUnit
我尝试运行简单的验证测试:
private function orderTickets($concert, $params)
{
$this->json('POST', "/concerts/{$concert->id}/orders", $params);
}
/** @test */
public function email_is_required_to_purchase_tickets()
{
$concert = factory(Concert::class)->create();
$this->orderTickets($concert, [
'ticket_quantity' => 3,
'payment_token' => $this->paymentGateway->getValidTestToken()
]);
$this->assertResponseStatus(422);
$this->assertArrayHasKey('email', $this->decodeResponseJson());
}
在最后一行我需要断言 json 有密钥 'email'。
但是当我 运行 它时,我收到这条消息:
Error: Call to undefined method Tests\Feature\PurchaseTicketsTest::decodeResponseJson()
我是否需要将任何东西导入我的 Laravel 项目才能正常工作?
好像完全不知道这个方法
我在Laravel 7
decodeResponseJson
是 Illuminate\Testing\TestResponse
的一部分,而不是 TestCase
。我将您的 orderTickets
方法更改为 return 响应,以便可以在测试中使用它。
private function orderTickets($concert, $params): TestResponse
{
return $this->json('POST', "/concerts/{$concert->id}/orders", $params);
}
/** @test */
public function email_is_required_to_purchase_tickets()
{
$concert = factory(Concert::class)->create();
$response = $this->orderTickets($concert, [
'ticket_quantity' => 3,
'payment_token' => $this->paymentGateway->getValidTestToken()
]);
self::assertEquals(422, $response->getStatusCode());
self::assertArrayHasKey('email', $response->decodeResponseJson());
}
我正在学习与您相同的“测试驱动 Laravel”课程,由于该课程是为 Laravel 5.x 编写的,所以我遇到了同样的问题。使用较新的 Laravel 7 测试函数实际上有更方便的方法来解决这个问题。无需解码响应并使用 assertArrayHasKey 检查它,您可以直接在响应上调用 assertJsonValidationErrors(响应已经继承自 \lluminate\Testing\TestResponse)。这是该测试函数的完整代码...
/** @test */
public function email_is_required_to_purchase_tickets(){
$paymentGateway = new FakePaymentGateway;
// Bind the FakePaymentGateway class to the PaymentGateway interface
// so we can type hint the interface in the controller methods. This
// should probably be moved to a service provider though.
$this->app->instance(PaymentGateway::class, $paymentGateway);
// Create a concert
$concert = factory(Concert::class)->create();
// Purchase concert tickets
$this->json('POST', "/concerts/{$concert->id}/orders", [
'ticket_quantity' => 3,
'payment_token' => $paymentGateway->getValidTestToken(),
]);
// Laravel uses response code 422 for validation error responses
$this->assertResponseStatus(422);
// Assert that the json response contains an email validation error.
$this->response->assertJsonValidationErrors('email');
}
我尝试运行简单的验证测试:
private function orderTickets($concert, $params)
{
$this->json('POST', "/concerts/{$concert->id}/orders", $params);
}
/** @test */
public function email_is_required_to_purchase_tickets()
{
$concert = factory(Concert::class)->create();
$this->orderTickets($concert, [
'ticket_quantity' => 3,
'payment_token' => $this->paymentGateway->getValidTestToken()
]);
$this->assertResponseStatus(422);
$this->assertArrayHasKey('email', $this->decodeResponseJson());
}
在最后一行我需要断言 json 有密钥 'email'。 但是当我 运行 它时,我收到这条消息:
Error: Call to undefined method Tests\Feature\PurchaseTicketsTest::decodeResponseJson()
我是否需要将任何东西导入我的 Laravel 项目才能正常工作?
好像完全不知道这个方法
我在Laravel 7
decodeResponseJson
是 Illuminate\Testing\TestResponse
的一部分,而不是 TestCase
。我将您的 orderTickets
方法更改为 return 响应,以便可以在测试中使用它。
private function orderTickets($concert, $params): TestResponse
{
return $this->json('POST', "/concerts/{$concert->id}/orders", $params);
}
/** @test */
public function email_is_required_to_purchase_tickets()
{
$concert = factory(Concert::class)->create();
$response = $this->orderTickets($concert, [
'ticket_quantity' => 3,
'payment_token' => $this->paymentGateway->getValidTestToken()
]);
self::assertEquals(422, $response->getStatusCode());
self::assertArrayHasKey('email', $response->decodeResponseJson());
}
我正在学习与您相同的“测试驱动 Laravel”课程,由于该课程是为 Laravel 5.x 编写的,所以我遇到了同样的问题。使用较新的 Laravel 7 测试函数实际上有更方便的方法来解决这个问题。无需解码响应并使用 assertArrayHasKey 检查它,您可以直接在响应上调用 assertJsonValidationErrors(响应已经继承自 \lluminate\Testing\TestResponse)。这是该测试函数的完整代码...
/** @test */
public function email_is_required_to_purchase_tickets(){
$paymentGateway = new FakePaymentGateway;
// Bind the FakePaymentGateway class to the PaymentGateway interface
// so we can type hint the interface in the controller methods. This
// should probably be moved to a service provider though.
$this->app->instance(PaymentGateway::class, $paymentGateway);
// Create a concert
$concert = factory(Concert::class)->create();
// Purchase concert tickets
$this->json('POST', "/concerts/{$concert->id}/orders", [
'ticket_quantity' => 3,
'payment_token' => $paymentGateway->getValidTestToken(),
]);
// Laravel uses response code 422 for validation error responses
$this->assertResponseStatus(422);
// Assert that the json response contains an email validation error.
$this->response->assertJsonValidationErrors('email');
}