我正在尝试为文章控制器测试非常简单的添加操作。以下是我的代码。我不明白为什么这不起作用。
$this->Articles->Article->hasAny
对于刚刚调用的admin_add操作和我刚刚添加的测试记录,应该返回true。但它返回false;
class ArticlesControllerTestCase extends ControllerTestCase {
/**
* Test Admin Add
*
* @return void
*/
public function testAdminAdd() {
#define sample passing data
$sampleDataPass = array(
'Article'=>array(
'title'=>'---Test Article Add---',
'body'=>'---Test Article Add Body---',
'status_id'=>1,
'category_id'=>1,
)
);
#test passing data
$this->Articles->request->data = $sampleDataPass;
$this->testAction('admin/articles/add', array('data'=>$sampleDataPass));
$this->assertEquals($this->Articles->Article->hasAny(array('Article.title'=>'---Test Article Add---')), true); #fails here
}
}
class MastersController extends AppController {
/**
* Admin Add
* Default admin add method for all controllers
* Adds a record to the models database table
* Not calling add action becase of __setRootUserId action
* @return void
*/
protected function _admin_add() {
if (!empty($this->request->data)) {
#save data
$this->{$this->modelClass}->create();
if ($this->{$this->modelClass}->save($this->request->data)) {
return $this->Redirect->flashSuccess($this->modelClass.' saved.', isset($this->redirectSuccessUrl) ? $this->redirectSuccessUrl : array('action' => 'view', $this->{$this->modelClass}->id));
} else {
return $this->Redirect->flashWarning($this->modelClass.' could not be saved.', isset($this->redirectWarningUrl) ? $this->redirectWarningUrl : null);
}
}
$this->_lists();
}
}
class ArticlesController extends MastersController {
/**
* Admin Add
* @see controllers/MastersController::_admin_add()
* @return void
*/
public function admin_add(){
parent::_admin_add();
}
}
答案 0 :(得分:0)
$this->Article
的缓存$this->controller
(仅在调用testAction后才能工作)。如果您之前需要调用它,则需要在执行任何操作之前调用$this->generate()
来获取控制器实例