【问题标题】:What's the difference between the `generate()` method and the new ControllerTestCase class in CakePHP 2?`generate()` 方法和 CakePHP 2 中新的 ControllerTestCase 类有什么区别?
【发布时间】:2012-01-03 15:42:34
【问题描述】:

关于 CakePHP 中的单元测试,它似乎是两种主要测试动作的方式。

第一个是使用generate() 方法模拟涉及的控制器:

$Posts = $this->generate('Posts', array(
  'methods' => array(
    'isAuthorized'
  ),
  'models' => array(
    'Post' => array('save')
  ),
  'components' => array(
    'RequestHandler' => array('isPut'),
    'Email' => array('send'),
    'Session'
)
));

但是in CakePHP 2 a new ControllerTestCase class has been added 显然简化了这个配置:

class PostControllerTest extends ControllerTestCase {

    public $fixtures = array('app.post');

    function testIndex() {
        $result = $this->testAction('/post/index');
        debug($result);
    }
    ...
}

我在通过 ControllerTestCase 类使用组件时发现了几个问题。我想知道这两种方法之间有什么区别(如果有的话)。

谢谢!

【问题讨论】:

    标签: php unit-testing cakephp controller components


    【解决方案1】:

    不,实际上是一样的,generate 方法用于在使用testAction 之前配置控制器,这样您就可以控制在测试操作之前应该模拟哪些组件、模型或控制器方法.方法generate 仅在ControllerTestCase 类中可用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 2015-04-18
      • 2016-06-28
      • 2012-03-27
      • 1970-01-01
      相关资源
      最近更新 更多