虚心请教

我看了好多遍,都没有找到问题的原由,请大家指点迷津!

    public function getWeather(string $city, string $type = 'base', string $format = 'json')
    {
        if (! \in_array(strtolower($format), ['json','xml'])) {
            throw new InvalidArgumentException('Invalid response format:'.$format);
        }
        if (! \in_array(strtolower($type), ['all','base'])) {
            throw new InvalidArgumentException('Invalid type (all/base):'.$type);
        }

        $url = 'https://restapi.amap.com/v3/weather/weatherInfo';

        $query = array_filter([
            'key' => $this->key,
            'city' => $city,
            'output' => \strtolower($format),
            'extensions' => \strtolower($type),
        ]);

        try {
            $response = $this->getHttpClient()->request("GET", $url, [
                'query' => $query
            ])->getBody()->getContents();

            return 'json' === $format ? \json_decode($response, true) : $response;
        } catch (\Exception $e) {
            throw new HttpException($e->getMessage(), $e->getCode(), $e);
        }
    }



    public function testGetWeather()

    {

        $response = new Response(200, [], '{"success": true}');

        $client = \Mockery::mock(Client::class);

        $client->allows()->request("GET", 'https://restapi.amap.com/v3/weather/weatherInfo', [

            'query' => [

                'key'=>'mock-key',

                'city' => '西宁',

                'output' => 'base',

                'extensions' => 'json'

            ]

        ])->andReturn($response);

        $weather = \Mockery::mock(Weather::class, ['mock-key'])->makePartial();

        $weather->allows()->getHttpClient()->andReturn($client);

        $this->assertSame(['success'=>true], $weather->getWeather('西宁'));

    }


There was 1 error:

1) DanceLynx\Weather\Tests\WeatherTest::testGetWeather
DanceLynx\Weather\Exceptions\HttpException: No matching handler found for Mockery_0_GuzzleHttp_Client::request('GET', '
https://restapi.amap.com/v3/weather/weatherInfo', ['query' => [...]]). Either the method was unexpected or its argument
s matched no expected argument list for this method

/home/vagrant/code/packages/weather/src/Weather.php:52
/home/vagrant/code/packages/weather/tests/WeatherTest.php:27

Caused by
Mockery\Exception\NoMatchingExpectationException: No matching handler found for Mockery_0_GuzzleHttp_Client::request('G
ET', 'https://restapi.amap.com/v3/weather/weatherInfo', ['query' => [...]]). Either the method was unexpected or its ar
guments matched no expected argument list for this method

/home/vagrant/code/packages/weather/vendor/mockery/mockery/library/Mockery/ExpectationDirector.php:92
/home/vagrant/code/packages/weather/src/Weather.php:47
/home/vagrant/code/packages/weather/tests/WeatherTest.php:27

ERRORS!
Tests: 3, Assertions: 4, Errors: 1.
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案

去掉 mock 然后在方法里 debug 一下最后真是调用的参数对比一下差异

4年前 评论
讨论数量: 1

去掉 mock 然后在方法里 debug 一下最后真是调用的参数对比一下差异

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!