虚心请教
我看了好多遍,都没有找到问题的原由,请大家指点迷津!
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.
推荐文章: