0
3

More than 3 years have passed since last update.

Mockery\Exception\NoMatchingExpectationException を とりあえず解決する方法

Last updated at Posted at 2021-03-08

Mockery をテストしているとやっているときに配列やオブジェクトを使っているとNoMatchingExpectationExceptionが出てきたときにどこが問題だかわからなくなる場合があります。

$this->guzzleClient = Mockery::mock(ClientInterface::class);
$requestResponse = $this->client->request('POST', $apiUrl, ['form_params' => $params]);
$this->guzzleClient
    ->shouldReceive('request')
    ->once()
    ->with('POST',
        'http://test.example.com/article/xxxx/comments',
        ['form_params' => $params])
    ->andReturn($response);
Mockery\Exception\NoMatchingExpectationException: No matching handler found for Mockery_0_GuzzleHttp_ClientInterface::request('POST', 'http://test.example.com/article/xxxx/comments', ['form_params' => [...]]). Either the method was unexpected or its arguments matched no expected argument list for this method

こういうとき、どの引数が一致せずにエラーが出るのかよくわからなくなります。
なので anyな引数を許可して実行してみることが大事です。

解決法: こんなときは Mockery::on を使ってすべての引数を許可する形にしてしまう。

どの引数が原因だかわからないときは、とりあえずエラーが出ずに動くようにすることを優先すると特定が早まった.

$this->guzzleClient = Mockery::mock(ClientInterface::class);
$requestResponse = $this->client->request(
    Mockery::on(function($actual) {
        return true;
    }),
    Mockery::on(function($actual) {
        return true;
    }), 
    Mockery::on(function($actual) {
        return true;
    })
);

例えば、弄っているうちに原因が第三引数にあることがわかったので下記のように修正すると↓

最終形はこんなの

$form_params = ['form_params' => $params];
$this->guzzleClient = Mockery::mock(ClientInterface::class);
$requestResponse = $this->client->request(
    'POST',
    $apiUrl,
    Mockery::on(function($actual) {
        return is_array($actual) && arrayHasKey($actual,'form_params') && is_array($actual['form_params']);
    })
);

参考文献:

その他

上記はチームメンバーの問題解決に口出ししただけなので、自分の手元で動かして動作確認してないことに注意

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3