更深入地了解这一点,我不确定是否有可能,这很遗憾,因为我正在尝试学习TDD。
我想用创建的Billable
并订阅计划来测试我的模型。
/** @test */
public function an_account_can_subscribe_to_a_plan() {
$account = factory("App\Account")->create();
Stripe::setApiKey('key');
$paymentMethod = PaymentMethod::create([
'type' => 'card',
'card' => [
'number' => '4242424242424242',
'exp_month' => '2',
'exp_year' => '2021',
'cvc' => '314'
]
]);
$subscription = $account->newSubscription('default', 'starter')->create($paymentMethod);
$this->assertTrue( $subscription->valid() );
}
Laravel Cashier文档显示了如何通过Stripe.js发送令牌,但这不适用于单元测试。
我试图直接包含Stripe库并创建一个PaymentMethod
对象,但这也需要我手动设置API密钥。现在我得到的错误是我必须验证我的电话号码才能将原始信用卡号发送到Stripe API。
我希望有更好的方法。如何以TDD方式使用Laravel Cashier并使用假付款方式模拟假订阅?
答案 0 :(得分:2)
条带不仅提供测试卡号,还提供令牌和付款方式。
https://stripe.com/docs/testing#cards
点击PaymentMethods标签。该值(例如pm_card_visa
)可以在测试中直接在服务器端使用,而无需前端付款意图实现。
这是我进行的功能测试的示例:
/**
* @test
*/
public function a_user_can_subscribe_to_a_paid_plan()
{
$this->actingAs($this->user);
$this->setUpBilling();
$enterprise = Plan::where('name', 'Enterprise')->first()->stripe_plan_id;
$response = $this->post(route('paywall.payment'), [
'payment_method' => 'pm_card_visa',
'stripe_plan_id' => $enterprise
])
->assertSessionDoesntHaveErrors()
->assertRedirect();
}
您的测试可能会有所不同,但是您可以使用这些测试付款方式向您的计费控制器发出正常的请求,就像您在前端进行的操作一样。
答案 1 :(得分:0)
您可能希望在测试过程中使用stripe-mock进行模拟。如果这不符合您的需求,那么mocking the objects directly可能是一个更好的选择。