Cakephp 2.4模拟模型中的方法
-
20-12-2019 - |
题
我想测试一个模型,并用于其中一个测试我想要模拟我正在测试的模型的方法。所以我不测试一个控制器,我不想替换整个模型,只有一个方法的一个方法,我测试了一个方法。
原因是此模型方法调用文件上传处理程序。此功能已在其他地方进行测试。
我现在正在做的是: 我测试模型'内容'。在那里,我测试它的方法'AddTeaser',调用'sendteaser'。 所以我想模仿SendTeaser并伪造方法SendTeaser的成功答案,而测试AddTeaser。
看起来像这样:
$model = $this->getMock('Content', array('sendTeaser'));
$model->expects($this->any())
->method('sendTeaser')
->will($this->returnValue(array('ver' => ROOT.DS.APP_DIR.DS.'webroot/img/teaser/5/555_ver.jpg')));
$data = array(
'Content' => array(
'objnbr' => '555',
'name' => '',
...
)
)
);
$result = $model->addTeaser($data);
$expected = true;
$this->assertEquals($expected, $result);
.
当我让我的测试运行时,我收到了一个错误,即方法'sendteaser'中的模型未正确调用。嘿!它不应该被称为!我嘲笑这种方法! .....或不呢?
嘲笑方法的正确语法?
非常感谢始终帮助!
灾难Jane
编辑: 以下是我模型的相关代码:
App::uses('AppModel', 'Model');
/**
* Content Model
*
* @property Category $Category
*/
class Content extends AppModel {
public $dateipfad = '';
public $fileName = '';
public $errormessage = '';
public $types = array(
'sqr' => 'square - more or less squarish',
'hor' => 'horizontal - clearly wider than high',
'lnd' => 'landscape - low but very wide',
'ver' => 'column - clearly higher than wide',
);
public $order = "Content.id DESC";
public $actsAs = array('Containable');
public $validateFile = array(
'size' => 307200,
'type' => array('jpeg', 'jpg'),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
public $hasMany = array(
'CategoriesContent' => array(
'className' => 'CategoriesContent',
),
'ContentsTag' => array(
'className' => 'ContentsTag',
),
'Description' => array(
'className' => 'Description',
)
);
/**
* Saves the teaser images of all formats.
*
* @param array $data
*
* @return Ambigous <Ambigous, string, boolean>
*/
public function addTeaser($data)
{
$objnbr = $data['Content']['objnbr'];
$type = $data['Content']['teaser-type'];
if (!empty($data['Content']['teaser-img']['tmp_name'])) {
$mFileNames = $this->sendTeaser($data, $objnbr, $type);
}
if (!is_array($mFileNames)) {
$error = $mFileNames;
//Something failed. Remove the image uploaded if any.
$this->deleteMovedFile(WWW_ROOT.IMAGES_URL.$mFileNames);
return $error;
}
return true;
}
/**
* Define imagename and save the file under this name.
*
* Since we use Imagechache, we don't create a small version anymore.
*
* @param integer $objnbr
* @param string $teasername
*
* @return multitype:Ambigous <string, boolean> |Ambigous <boolean, string>
*/
public function sendTeaser($data, $objnbr, $type)
{
//$path = str_replace('htdocs','tmp',$_SERVER['DOCUMENT_ROOT']);
$this->fileName = $this->getImageName($objnbr, $type);
$oUH = $this->getUploadHandler($data['Content']['teaser-img']);
debug($oUH);
exit;
$error = $oUH->handleFileUpload();
if (empty($type))
$type = 0;
if ($error === 'none'){
// Send to ImageChacheServer
$oICC = $this->getImagecacheConnector();
$sCacheUrl = $oICC->uploadFile($objnbr, $type, $this->fileName);
debug($sCacheUrl);
return array($type => $this->fileName);
}
return $error;
}
public function getUploadHandler($imgdata)
{
App::uses('UploadHandler', 'Lib');
$oUH = new UploadHandler($this, $imgdata);
return $oUH;
}
}
.
更改GetMock到GetMockFormodel的情况下也没有改变输出。
解决方案
$ this-> getmock不是模拟的方式。您应该使用$ this-> generate
我会推荐你读一本关于cakephp unti测试的书,如下所示: https://leanpub.com/cakephpunittesting
不隶属于 StackOverflow