testAction() function returns null on debug() in CakePhp testing(testAction() 函数在 CakePhp 测试中的 debug() 上返回 null)
问题描述
I was trying to learn how to use unit testing in CakePhp, I'm trying to write a controller test. I read about testAction() and debug() function, but It doesn't work for me, I mean, the test method passes, but the debug() returns null (because 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);        
    }
}
Posts/index controller returns a list of all posts stored in the DB.
I'm assuming you're using CakePHP 2.
$this->testAction() can return a few different results, depending on the options you give it.
For example, if you set the return option to vars, the testAction() method will return an array of the vars that have been set in the tested action:
public function testIndex() {
    $result = $this->testAction('/posts/index', array('return' => 'vars'));
    debug($result);
}
In this example, the debug data should be an array of the vars that you set in the /posts/index action.
The CakePHP documentation describes the possible results that you can have returned here: http://book.cakephp.org/2.0/en/development/testing.html#choosing-the-return-type
Note that the default option, result, gives you the value that your controller action returns. For most controller actions this will be null, so the fact that you're getting null in your example is to be expected.
这篇关于testAction() 函数在 CakePhp 测试中的 debug() 上返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:testAction() 函数在 CakePhp 测试中的 debug() 上返回 null
				
        
 
            
        基础教程推荐
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
 - 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
 - php中的PDF导出 2022-01-01
 - 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
 - php中的foreach复选框POST 2021-01-01
 - PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
 - Yii2 - 在运行时设置邮件传输参数 2022-01-01
 - Web 服务器如何处理请求? 2021-01-01
 - 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
 - 将变量从树枝传递给 js 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				