0
0

More than 3 years have passed since last update.

Mockeryでメソッドチェーンのテスト

Posted at

このようなメソッドチェーンしてるPHPコードのテストコードが書きたいとき、

$this->logger
    ->channel('channel_name')
    ->info('success!', ['id' => 1]);

一旦andReturnSelf()して自身を返せば良い。

$this->logger = m::mock(LoggerInterface::class);

$this->logger
    ->shouldReceive('channel')
    ->with('channel_name')
    ->andReturnSelf()
    ->once();

$this->logger
    ->shouldReceive('info')
    ->with('success!', ['id' => 1])
    ->once();
0
0
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
0