我正在使用 Laravel 6 并尝试测试端点。端点向外部 API 发出 2 个请求(来自 mollie)。目前我像这样嘲笑它:
抽象基MollieEndpointTest
<?php
namespace Tests;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Mollie\Api\MollieApiClient;
abstract class BaseMollieEndpointTest extends TestCase
{
/**
* @var Client|\PHPUnit_Framework_MockObject_MockObject
*/
protected $guzzleClient;
/**
* @var MollieApiClient
*/
protected $apiClient;
protected function mockApiCall(Response $response)
{
$this->guzzleClient = $this->createMock(Client::class);
$this->apiClient = new MollieApiClient($this->guzzleClient);
$this->apiClient->setApiKey('test_dHar4XY7LxsDOtmnkVtjNVWXLSlXsM');
$this->guzzleClient
->expects($this->once())
->method('send')
->with($this->isInstanceOf(Request::class))
->willReturnCallback(function (Request $request) use ($response) {
return $response;
});
}
}
我所有的测试都从那个 ^ 抽象类扩展而来。我这样实现它:
public function test()
{
$this->mockApiCall(
new Response(
200,
[],
'{
"response": "here is the response",
}'
)
);
Mollie::shouldReceive('api')
->once()
->andReturn(new MollieApiWrapper($this->app['config'], $this->apiClient));
dd(Mollie::api()->customers()->get('238u3n'));
}
这是有效的。但问题是当我需要在同一个 api 调用中模拟另一个请求时,我会得到相同的结果。
那么如何确保我可以模拟 2 个响应(而不是 1 个)并将其返回给特定的 url?