【问题标题】:How to include a model into a Yii unit test file如何将模型包含到 Yii 单元测试文件中
【发布时间】:2014-06-23 14:38:33
【问题描述】:

我需要包含一个模型,这样我就可以在我的单元测试中创建一个新的 SomeClass。我虽然它会自动为我这样做,但给我一个错误

找不到类 SomeClass

【问题讨论】:

    标签: unit-testing yii phpunit


    【解决方案1】:

    这是因为你的类没有被自动加载到你的测试中;

    在您的应用程序文件夹中,您将拥有类似的内容

     protected/                containing protected application files
          tests/                 containing tests for the application
             fixtures/           containing database fixtures
             functional/         containing functional tests
             unit/               containing unit tests
             report/             containing coverage reports
             bootstrap.php       the script executed at the very beginning
             phpunit.xml         the PHPUnit configuration file
             WebTestCase.php     the base class for Web-based functional tests
    

    bootstrap.php 包含自动加载所有类(如 index.php)的入口脚本;它看起来像这样

    $yiit='path/to/yii/framework/yiit.php';
    $config=dirname(__FILE__).'/../config/test.php';
    require_once($yiit);
    require_once(dirname(__FILE__).'/WebTestCase.php');
    Yii::createWebApplication($config);
    

    检查path/to/framework/yiit.php是否配置到包含框架的正确目录

    您需要将单元测试存储在 protected/tests/unit 中;如果您正在测试模型,理想情况下测试类从 CDbTestCase 扩展

    从你运行的测试目录运行一个类的测试

    phpunit -- unit/MyClassTest.php

    您应该阅读指南中的测试章节以完全理解这一点:http://www.yiiframework.com/doc/guide/1.1/en/test.overview

    你没有使用 yii 测试框架进行单元测试;您当然可以使用自己的自动加载器和入口脚本设置phpunit 以仅加载特定的类等(提高测试速度),但是只有在您知道自己在做什么的情况下才应该这样做;使用 Yii 测试框架更容易;

    【讨论】:

      猜你喜欢
      • 2012-05-09
      • 1970-01-01
      • 2014-05-10
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      • 2012-07-30
      • 2016-02-23
      • 2012-06-15
      相关资源
      最近更新 更多