2013-10-25 7 views
0

Predis를 사용하여 Redis DB에 연결하는 클래스에 대한 일부 단위 테스트를 설정하고 있습니다. 내가 단위 테스트를 할 수 있도록 클라이언트 및 연결 개체를 조롱하고있어. 장치 내부PHP Mockery가 조롱 된 함수에서 조롱 된 객체를 반환하지 않음

$redisConnection = m::mock('overload:Predis\Connection\SingleConnectionInterface') 
     ->shouldReceive('isConnected') 
     ->once() 
     ->andReturn(true) 
     ->getMock(); 

    $redis = m::mock("Predis\Client") 
     ->shouldReceive(array('getConnection' => $redisConnection)) 
     ->once() 
     ->getMock(); 

    // var_dump($redis->getConnection()); -> $redisConnection 

    $instance = new Class($redis); 

:

public function __construct(Predis\Client $redis = null){ 
    $this->_redis = $redis; 
    $this->_validateConnection(); 
} 

private function _validateConnection(){ 
    $connnection = $this->_redis->getConnection(); 

    // var_dump($connection); -> null 

    if (!$connection->isConnected()){ 
     throw new InvalidDatabase("Unable to connect to the Redis database!"); 
    } 
} 

이 내가 단위 테스트를 설정하기 위해 노력하고있어 방법은 다음과 같습니다

은 Predis 클라이언트 오브젝트를 다루는 내 클래스의 섹션 테스트, $redis->getConnection()$redisConnection 개체를 반환하지만, 단위 테스트를 수행하는 클래스에 들어가면 $redis->getConnection()은 NULL을 반환합니다.

내가 무엇을 놓치고 싶다 아이디어는 있습니까?

답변

1

아마 그냥이 필요합니다

$redis = m::mock("Predis\Client") 
    ->shouldReceive(array('getConnection' => $redisConnection)) 
    ->once() 
    ->andReturn('whatever you want to return?') // missing a return... by default it will return null. 
    ->getMock(); 
관련 문제