1

I have installed php unit in my local server, but I dont understand (reading the php unit help) how to test my action create. My action is this, and the only thing I want to test is if it saves on database.

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

How will be the test method? something like this?

public function actionCreateTest(){
$model = new Model; 
$this->asserttrue($model->save());
}
emmanuel sio
  • 1,935
  • 3
  • 24
  • 26

1 Answers1

1

write functional tests for testing controllers functionality instead of unit tests,also

the thing that you are asserting here

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

if the outcome of $controller->actionCreate() is the value true, which is not!

you are $this->renderPartial() in that and returning nothing, so that statement will never be true.

Developerium
  • 7,155
  • 5
  • 36
  • 56
  • yes, Sorry you are in rigth, is my mistake. So I can change the code like this? $this->assertTrue($model->save()); (after setting my model) – emmanuel sio Feb 15 '14 at 05:50
  • are functional test better than unit test? I m a begginer in php unit, and maybe functional test are difficult to develope – emmanuel sio Feb 15 '14 at 05:52
  • for first comment yes, that would be correct use of assert functions, second http://stackoverflow.com/questions/2741832/unit-tests-vs-functional-tests , http://stackoverflow.com/questions/4904096/whats-the-difference-between-unit-functional-acceptance-and-integration-test – Developerium Feb 15 '14 at 05:56