我試圖在PHPUnit測試中模擬出Predis客戶端。當我調用我試圖模擬出的方法時,在測試結束時,PHPUnit告訴我期望沒有得到滿足。爲什麼我的PHPUnit模擬的Predis客戶端不符合我的期望?
下面是重現我的問題代碼示例:
class MockRedisTest extends \PHPUnit_Framework_TestCase {
private $mockRedis;
public function testMockRedis() {
$mockRedis = $this->getMock('Predis\\Client');
$mockRedis->expects( $this->once())
->method("exists")
->with($this->equalTo("query-key"))
->will($this->returnValue(true));
$mockRedis->exists("query-key");
}
}
並且PHPUnit會認爲該方法不叫:
1)MockRedisTest :: testMockRedis 預期失敗方法名稱等於被調用1次(s)。方法預計被稱爲1次,實際稱爲0次。
爲什麼?是否因爲Predis客戶端似乎在使用__call來響應匹配redis命令的方法調用?
更新:我得到的印象是它與__call方法有關。更改代碼到這個作品:
public function testMockRedis() {
$mockRedis = $this->getMock('Predis\\Client');
$mockRedis->expects( $this->once())
->method("__call")
->with("exists", $this->equalTo(array("query-key")))
->will($this->returnValue(true));
$mockRedis->exists("query-key");
}
不知道我對此感到滿意。有沒有更好的方法來模擬使用__call代理方法的類?
完美。謝謝。 –