【问题标题】:Models unit testing on Yii2Yii2 上的模型单元测试
【发布时间】:2015-01-16 21:28:18
【问题描述】:

我正在尝试通过单元测试构建 Yii2 应用程序,对此我有一些疑问。

class UsersTest extends \Codeception\TestCase\Test
{
   /**
    * @var \UnitTester
    */
    protected $users;

    protected function _before()
    {
        $this->users = new \app\models\Users;
    }

    protected function _after()
    {
    }

    // tests
    public function testGeId()
    {

    }

}

当我尝试运行这个测试类时,我收到了找不到用户类的致命错误消息。问题的原因是什么以及如何解决?

Yii2 测试文件夹中有自述文件,告诉我们设置 yii2-faker 和 yii2_basic_tests 数据库。这两个东西是什么,为什么要使用它们?

谢谢。

【问题讨论】:

    标签: unit-testing phpunit yii2 codeception


    【解决方案1】:

    需要在tests/_bootstrap.php中创建应用实例。它必须是该文件中的以下代码:

    require('/../vendor/autoload.php');
    require('/../vendor/yiisoft/yii2/Yii.php');
    
    $config = require('config/web.php');
    
    (new yii\web\Application($config));
    

    【讨论】:

      【解决方案2】:

      可能是你

      settings:
          bootstrap: _bootstrap.php
      

      在 codeception.yml 中有错误吗?该文件包括 vendor/autoload.php 和解析的类名

      【讨论】:

      • 引导是对的。是的,我的引导文件包括 vendor/autoload.php
      • 这个设置没有很好的记录。好东西你发布了这个。
      【解决方案3】:

      在phpunit xml配置文件中定义了自动加载器

      <?xml version="1.0" encoding="utf-8" ?>
      <phpunit bootstrap="./vendor/autoload.php">
      
          <testsuites>
              <testsuite name="The project's test suite">
                  <directory>./tests</directory>
              </testsuite>
          </testsuites>
      
      </phpunit>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-09
        • 1970-01-01
        • 2019-06-21
        • 2015-06-24
        • 2017-12-19
        • 2010-12-26
        • 2018-10-04
        • 1970-01-01
        相关资源
        最近更新 更多