【问题标题】:Unit-testing scripts in CakePHP applicationCakePHP 应用程序中的单元测试脚本
【发布时间】:2012-12-29 14:32:59
【问题描述】:

我使用 CakePHP 开发了一个交易应用程序。现在我想使用 PHPUnit 编写单元脚本。我已经在我的服务器上安装了 PHPUnit,并且测试核心测试工作正常。还安装了 Xdebug 用于代码分析。当我要为现有应用程序编写脚本时,它不起作用。我能够在模型中编写登录方法的单元脚本。但无法为其余方法编写脚本。

<?php

App::uses('User', 'Model');

class UserTest extends CakeTestCase {
    public $fixtures = array('app.user');
    public $dropTables = false;
    public function setUp() {
        parent::setUp();
        $this->User = ClassRegistry::init('User');
    }

    public function testLogin() {
         $result = $this->User->find('count', array(
            'conditions' => array(
                // making some assumptions about the test data here
                'email' => 'test.user@gmail.com',
                'password' => 'f1054da373ace628dc73b8ec52eb28072b074940',
            ),));
        $expected = 1;
        $this->assertEquals($expected, $result);
    }

}
?>

它运行良好。但我无法为其余方法编写脚本。

【问题讨论】:

  • 你为什么用$dropTables = false;
  • 您的第一个测试,testLogin() 实际上并没有测试您的任何代码。发布一个实际的测试方法并告诉我们你不能测试什么方法(或者为什么你不能测试它)会很有用。

标签: unit-testing cakephp phpunit


【解决方案1】:

尝试使用 bake 和 bake 测试以开始使用正确的测试结构:

cake bake test

这可能有助于按照 Cake 期望的测试结构方式将您推向写入方向。它还将创建空方法来测试控制器中的所有方法。

您能否更具体地说明什么不起作用?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 2017-08-05
    • 2015-08-13
    • 2022-10-14
    • 1970-01-01
    相关资源
    最近更新 更多