【问题标题】:How to test a method who calls an API? (Laravel)如何测试调用 API 的方法? (拉拉维尔)
【发布时间】:2020-05-08 21:34:29
【问题描述】:

我正在测试一种存储信用卡信息的方法,在该方法中我必须调用 API 来请求银行存储所有信用卡信息,然后他们返回一个令牌以进行未来交易。

问题是当我测试方法时出现此错误

cURL 错误 60:SSL 证书问题:证书链中的自签名证书

这是我的测试方法

/**
 ** @test
 **/
public function a_logged_user_can_add_a_credit_debit_card()
{
  $demoCard = [
        'card_number' => '5424180279791732',
        'month' => '05',
        'year' => '2021',
        'cvc' => '123',
    ];

    $reponse = $this->postJson(route('card.create'),
        $demoCard, $this->user->headersToken());

    $reponse->assertJson(['success' => true]);

    $this->assertEquals(1, Tarjeta::first()->user_id);
    $this->assertEquals($this->tarjetaDemo['month'], Tarjeta::first()->month);
    $this->assertEquals($this->tarjetaDemo['year'], Tarjeta::first()->year);
    $this->assertEquals($this->tarjetaDemo['cvc'], Tarjeta::first()->cvc);
    $this->assertTrue(!empty(Tarjeta::first()->token));
}

这是我的方法

 private $clientEcomm;
/**
 * constructor.
 */
public function __construct()
{
    $merchant = env('MERCHANT_ECOMM');
    $apiPassword = env('API_ECOMM_PASSWORD');
    $apiUsername = env('API_ECOMM_USER');

    $this->clientEcomm = new Client([
        'base_uri' => "https://banamex.dialectpayments.com/api/rest/version/54/merchant/{$merchant}/",
        'auth' => [$apiUsername, $apiPassword]
    ]);
}

public function create(CardRequest $request)
{

    $response = $this->clientEcomm->put(
        "token",
        [
            'json' => [
                'sourceOfFunds' => [
                    'provided' => [
                        'card' => [
                            'number' => $request->get('card_number'),
                            'expiry' => [
                                'month' => $request->get('month'),
                                'year' => Str::substr($request->get('year'), -2),
                            ],
                            'securityCode' => $request->get('cvc')
                        ]
                    ],
                    'type' => 'CARD',
                ],
            ]
        ]
    );

    $response = \GuzzleHttp\json_decode($response->getBody()->getContents());

    Tarjeta::create([
        'user_id' => \Auth::user()->id,
        'mes' => $request->get('month'),
        'year' => $request->get('year'),
        'cvc' => $request->get('cvc'),
        'token' =>  $response->token,
    ]);

    return $this->successResponse();
}

我怎样才能做这种测试?

【问题讨论】:

  • 检查一下,可能会有所帮助:Here
  • 实际上,当我使用邮递员检查该端点时它工作正常时,问题只是在我测试时,我想应该是另一种方式,比如嘲弄或类似的东西

标签: php laravel testing phpunit tdd


【解决方案1】:

当您为端点编写测试时,您需要小心模拟对第三方 API 的请求。因此,在这种情况下,您需要模拟 Client。

public function __construct(Client $client)
{

}
$this->app->instance(Client::class, Mockery::mock(Client::class, function($mockery){
    $mockery->shouldReceive('put')->once()->with($parameters)->andReturn($response);
}));

这样,您的代码将永远不会调用第三方端点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-16
    • 2016-08-27
    • 2020-08-27
    • 2018-06-08
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多