【发布时间】:2020-01-08 06:10:18
【问题描述】:
有 1 个错误:
1) 查看ConcertListingTest::customer_can_purchase_concert_tickets ErrorException: 类 App\Billing\FakePaymentGateway 的对象无法转换为字符串
正在做测试,但不知道它在抱怨什么......
<?php
namespace App\Billing;
class FakePaymentGateway implements PaymentGateway
{
private $charges;
public function __construct()
{
$this->charges = collect();
}
public function getValidTestToken()
{
return "valid-token";
}
public function charge($amount, $token)
{
$this->charges[] = $amount;
}
public function totalCharges()
{
return $this->charges->sum();
}
}
这是它的另一部分 购买TiketTest.php
<?php
class ViewConcertListingTest extends TestCase
{
use DatabaseMigrations;
/**
* @test
*
* @return void
*/
public function customer_can_purchase_concert_tickets()
{
$paymentGateway = new FakePaymentGateway;
$this->app->instance(PaymentGateway::class, $paymentGateway);
//dd($paymentGateway);
// Arrange
//Create a concert
$concert = factory(Concert::class)->create(['ticket_price' => 3250 ]);
// Act
// View the concert listing
$response = $this->json('POST', "/concerts/{$concert->id}/orders", [
'email' => 'john@example.com',
'ticket_quantity' => 3,
'payment_token' => $paymentGateway->getValidTestToken(),
] );
$response->assertStatus(201);
$this->assertEquals( 9750 , $paymentGateway->totalCharges() );
$order = $concert->orders()->where('email')->first();
$this->$this->assertNotNull($order);
$this->assertEquals( 3, $order->tickets->count() );
}
}
第 18 行带有注释的订单控制器
class ConcertsOrdersController extends Controller
{
private $paymentGateway;
public function __construct(PaymentGateway $paymentGateway)
{
$this->$paymentGateway = $paymentGateway; // Line 18
}
//
public function store($concertId)
{
$concert = Concert::find($concertId);
$ticketQuantity = request('ticket_quantity');
$amount = $ticketQuantity * $concert->ticket_price;
$token = request('payment_token');
$this->paymentGateway->charge($amount, $token);
$concert->orders()->create(['email' => request('email')]);
return response()->json([], 201);
}
}
预计通过结果
【问题讨论】:
-
1.你的测试文件在哪里。 2.发布完整的错误消息。它包含发生错误的行,它不是随机数,它意味着什么。
-
看到错误堆栈跟踪的第一行指向您的控制器吗?发布您的控制器(请指出第 18 行)
-
感谢您的时间,因为我很困惑,这方面的教程视频似乎较旧,并且与它们的外观不符......
-
``` interface PaymentGateway { public function charge($amount, $token); } ```
标签: php laravel object payment-gateway