【问题标题】:Yii php unit test action createYii php 单元测试动作创建
【发布时间】:2014-03-14 15:50:06
【问题描述】:

我已经在本地服务器中安装了 php 单元,但我不明白(阅读 php 单元帮助)如何测试我的操作创建。我的操作是这样的,我唯一想测试的是它是否保存在数据库中。

 /**
 * 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);
}

测试方法如何?像这样?

public function actionCreateTest(){
$model = new Model; 
$this->asserttrue($model->save());
}

【问题讨论】:

  • 你的模型调用正常吗...还是有问题...?

标签: unit-testing yii phpunit


【解决方案1】:

编写功能测试来测试控制器功能而不是单元测试,也

你在这里断言的东西

$this->assertEquals(true,$controller->actionCreate());

如果$controller-&gt;actionCreate() 的结果是值true,则不是!

你是 $this-&gt;renderPartial() 并且什么都不返回,所以这个陈述永远不会是真的。

【讨论】:

  • 是的,对不起,你说得对,是我的错误。所以我可以像这样更改代码吗? $this->assertTrue($model->save()); (设置我的模型后)
  • 功能测试比单元测试好吗?我是php单元的初学者,可能功能测试很难开发
  • 对于第一个评论是的,那将是正确使用断言函数,第二个stackoverflow.com/questions/2741832/…stackoverflow.com/questions/4904096/…
猜你喜欢
  • 2014-03-14
  • 2012-06-15
  • 2012-10-23
  • 2014-06-20
  • 2010-09-28
  • 2013-09-28
  • 1970-01-01
  • 1970-01-01
  • 2014-05-10
相关资源
最近更新 更多