开发者社区 问答 正文

狂饮后的请求没有嘲笑

我正在尝试为使用Guzzle http客户端的端点编写测试用例。但这并没有被嘲笑。它仍然符合实际要求并等待响应。我的代码如下。

测试用例

    new Response(200, ['X-Foo' => 'Bar'], 'Hello, World'),
]);
$handler = HandlerStack::create($mockHandler);
$client = new Client(['handler' => $handler]);

$this->app->instance(\GuzzleHttp\Client::class, $client);

$response = $this->actingAs($user)
            ->get('/api/request?' . http_build_query($request));

我在服务类中使用Guzzle客户端的方式如下

{
    public function __construct(\GuzzleHttp\Client $client, $extraInfo)
    {
        $this->client = $client;
        $this->setExtraInfo($extraInfo);
    }

    private function getToken($scope)
    {
        $response = $this->client->post(
            '/post/request/url',
            [
                'form_params' => [
                    "param_1" => "value_1",
                    "param_2" => 'value_2'
                ],
                'headers' => [
                    "Authorization"=> "Basic 5235252333",
                    "Content-Type" => 'application/x-www-form-urlencoded'
                ],
                self::COMMON_REQUEST_PARAMS,
                'cert' => ['cert path' , 'pass phrase']
            ]
        );

        $jsonOutput = json_decode($response->getBody()->getContents());

        return $jsonOutput->access_token;
    }
}

展开
收起
社区秘书 2019-12-09 16:58:49 631 分享 版权
阿里云 AI 助理回答

在你的测试用例中,你已经成功地设置了Guzzle HTTP客户端的模拟处理器(mock handler)来返回一个预设的响应。然而,问题在于尽管你替换了应用中的Guzzle客户端实例,但服务类中直接注入了\GuzzleHttp\Client,这绕过了你在测试中设置的替换逻辑。

为了解决这个问题,你需要确保在测试环境中注入的是你配置好的、带有模拟响应的Guzzle客户端实例。一种方法是利用依赖注入容器(Dependency Injection Container, DIC)的功能,在测试中临时替换或修改服务提供者中的客户端绑定。

以下是一个可能的解决方案:

  1. 创建一个可被替换的 Guzzle 客户端服务

    在Laravel的服务提供者中,定义一个新的服务来提供Guzzle客户端,这样你就可以在测试时轻松替换它。

    // app/Providers/AppServiceProvider.php (or a new Service Provider)
    
    public function register()
    {
       $this->app->singleton('guzzle.client', function ($app) {
           return new \GuzzleHttp\Client();
       });
    }
    
  2. 更新服务类以使用服务容器提供的客户端

    修改你的服务类,使其通过依赖注入的方式使用这个新定义的服务而非直接依赖 \GuzzleHttp\Client 类。

    use GuzzleHttp\ClientInterface;
    
    class YourService
    {
       public function __construct(ClientInterface $client, $extraInfo)
       {
           $this->client = $client;
           $this->setExtraInfo($extraInfo);
       }
    
       // ...其他代码保持不变
    }
    
  3. 在测试中替换服务容器中的 Guzzle 客户端

    现在,在你的测试用例中,你可以直接替换服务容器中的 guzzle.client 实例,而不需要再手动实例化和替换 \GuzzleHttp\Client

    public function testYourEndpoint()
    {
       $mockHandler = new MockHandler([
           new Response(200, ['X-Foo' => 'Bar'], 'Hello, World'),
           // 添加更多预期响应,如果需要的话
       ]);
    
       $handlerStack = HandlerStack::create($mockHandler);
       $mockClient = new Client(['handler' => $handlerStack]);
    
       // 使用服务容器替换Guzzle客户端实例
       $this->app->instance('guzzle.client', $mockClient);
    
       // 余下的测试逻辑...
       $response = $this->actingAs($user)
                        ->get('/api/request?' . http_build_query($request));
    
       // 验证响应...
    }
    

通过这种方式,你的服务类将使用测试中设定的模拟客户端,从而避免了实际的网络请求,并允许你精确控制测试中的响应行为。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答
问答分类:
问答地址: