假设我有以下课程:
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 的实例,而是找到了模拟对象。任何人都可以阐明如何测试这种情况吗?在使用模拟和存根进行测试时,我是初学者。
谢谢!