在Mockery中是否有一个setMethods()等效于Mocking Chained Methods

时间:2015-03-20 18:38:19

标签: php unit-testing mocking phpunit mockery

我正在嘲笑一系列方法。我可以用PHPUnit的MockBuilder让它工作得很好;但是,我想知道是否有更简洁的方法在Mockery中嘲笑它们。

我目前的代码如下:

    $this->RepositoryMock = $this->getMockBuilder('Repository')
        ->setMethods(array('method1', 'method2', 'method3'))
        ->getMock();


    $this->RepositoryMock->expects($this->any())
        ->method('method1')
        ->will($this->returnSelf());
    $this->RepositoryMock->expects($this->any())
        ->method('method2')
        ->will($this->returnValue(true));
    $this->RepositoryMock->expects($this->any())
        ->method('method3')
        ->will($this->returnValue(true));

setMethods在使用Mockery :: mock时不起作用,但我希望它看起来像下面的代码用Mockery的版本替换setMethods:

    $this->RepositoryMock = Mockery::mock('Repository')
         ->setMethods('method1', 'method2', 'method3');

    $this->RepositoryMock
         ->shouldReceive('method1')
         ->andReturn($this->returnSelf())
         ->shouldReceive('method2')
         ->andReturn(true)
         ->shouldReceive('method3')
         ->andReturn(true)

1 个答案:

答案 0 :(得分:0)

我想通了,并且setMethods()不是必需的,它们可以在没有的情况下被链接在一起。

$this->RepositoryMock = Mockery::mock('Repository');

$this->RepositoryMock
     ->shouldReceive('method1')
     ->andReturn($this->returnSelf())
     ->shouldReceive('method2')
     ->andReturn(true)
     ->shouldReceive('method3')
     ->andReturn(true)