Unit testing Yii php单元测试动作创建
我已经在本地服务器上安装了php单元,但我不明白(阅读php单元帮助)如何测试我的创建操作。我的操作就是这样,我唯一要测试的是它是否保存在数据库中Unit testing Yii php单元测试动作创建,unit-testing,yii,phpunit,Unit Testing,Yii,Phpunit,我已经在本地服务器上安装了php单元,但我不明白(阅读php单元帮助)如何测试我的创建操作。我的操作就是这样,我唯一要测试的是它是否保存在数据库中 /** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $_class = $this->getC
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate() {
$_class = $this->getClassName();
$model = new $_class;
if (isset($_POST)) {
$model->attributes = $_POST;
$this->armaMensajeABMGrilla($model->save(), $model);
}
$this->renderPartial('create', array(
'model' => $model,), false, true);
}
protected function armaMensajeABMGrilla($guardoOk, $modelo = null) {
if ($guardoOk == true) {
$this->respuestaJSON = array('success' => true, 'mensaje' => 'ok');
} else {
$erroresMensaje = 'Listado de Errores: <br/><ul>';
$i = 0;
if (isset($modelo->errors)) {
foreach ($modelo->errors as $error) {
$erroresMensaje .= '<li>Error(' . $i . '): ' . $error[0] . '</li>';
$i++;
}
$erroresMensaje.='</ul>';
}
$this->respuestaJSON = array('success' => false, 'mensaje' => $erroresMensaje);
}
$this->renderJSON($this->respuestaJSON);
}
编写功能测试来测试控制器功能,而不是单元测试 你在这里主张的东西
$this->assertEquals(true,$controller->actionCreate());
如果$controller->actionCreate()
的结果是值true
,则不是
你是
$this->renderPartial()
在那里面,什么也不返回,所以这个语句永远不会是真的。你的模型叫ok吗。。。或者它有问题…?是的,对不起,你是正确的,这是我的错误。所以我可以像这样修改代码$此->assertTrue($model->save());(设置模型后)功能测试是否优于单元测试?我是php单元的乞丐,可能功能测试很难开发,第一条评论是的,这将是正确使用断言函数,第二条,
$this->assertEquals(true,$controller->actionCreate());