說我有下面的類:PHPUnit的:通過模擬對象作爲方法參數
class Document
{
private file;
public function setFile(UploadedFile $file)
{
$this->file = $file;
}
public function getExt()
{
return $this->file->guessExtension();
}
}
我想測試getExt()方法。我試圖建立測試如下:
$file = $this->getMock('UploadedFile');
$file->expects($this->at(0))
->method('guessExtension')
->will($this->returnValue('png'));
$doc = new Document();
$doc->setFile($file);
...
但是,它給我的錯誤說setFile()期待的UploadedFile的一個實例,模擬對象發現來代替。任何人都可以闡明如何測試這種情況?當涉及到模擬和存根測試時,我是一名初學者。
謝謝!
,不是應該是一個問題,生成的模擬類繼承了原始類。你偶然使用命名空間嗎? –