TestAction () returns null for debug () in CakePhp

I was trying to learn how to use unit testing in CakePhp, I am trying to write a controller test. I read about the functions testAction () and debug (), but this does not work for me, I mean the test method passes, but debug () returns null (as testAction does)

This is my code:

<?php
App::uses('Controller', 'Controller');
App::uses('View', 'View');
App::uses('PostsController', 'Controller');

class PostsControllerTest extends ControllerTestCase {
    public function setUp() {
       parent::setUp();
       $Controller = new Controller();
       $View = new View($Controller);
       $this->Posts = new PostsController($View);
    }

    public function testIndex() {
          $result = $this->testAction('Posts/Index');
        debug($result);        

    }
}

The mail / index controller returns a list of all messages stored in the database.

+5
source share
2 answers

I assume you are using CakePHP 2.

$this->testAction() may return slightly different results, depending on the parameters that you give it.

, return vars, testAction() , :

public function testIndex() {
    $result = $this->testAction('/posts/index', array('return' => 'vars'));
    debug($result);
}

, /posts/index.

CakePHP , : http://book.cakephp.org/2.0/en/development/testing.html#choosing-the-return-type

, result , . null, , null .

+10

mtnorthrop , . , testAction ('/action', array ('return' = > 'contents') null. :

: CakePHP Unit Test AppController:: beforeFilter(), , , :

// For Mock Objects and Debug >= 2 allow all (this is for PHPUnit Tests)
if(preg_match('/Mock_/',get_class($this)) && Configure::read('debug') >= 2){
    $this->Auth->allow();
}

, : https://groups.google.com/forum/#!topic/cake-php/eWCO2bf5t98 Auth ControllerTestCase:

class MyControllerTest extends ControllerTestCase {
    public function setUp() {
        parent::setUp();
        $this->controller = $this->generate('My',
            array('components' => array(
                'Auth' => array('isAuthorized')
            ))
        );
        $this->controller->Auth->expects($this->any())
            ->method('isAuthorized')
            ->will($this->returnValue(true));

    }
}

( CakePhp 2.3.8)

+1

All Articles