繁体   English   中英

为模块yii2创建测试

[英]Creating tests for modules yii2

嗨,我是yii2测试的新手,我在应用程序/测试中设置了测试环境并进行了测试,并且工作正常。 现在,我想为我的一个模块(模块/ v1)设置测试,其中包含命令,组件,控制器和模型文件夹。 我已使用codecept bootstrap为模块/ v1中的测试创建了初始设置,并按以下步骤进行设置

codeception.yml

namespace: v1
paths:
    tests: tests
    output: tests/_output
    data: tests/_data
    support: tests/_support
    envs: tests/_envs
actor_suffix: Tester
settings:
    bootstrap: /../_bootstrap.php
    colors: true
    memory_limit: 1024M
extensions:
    enabled:
        - Codeception\Extension\RunFailed
modules:
    enabled:
        - Yii2:
            configFile: '/../../config/test.php'

测试/ unit.suite.yml

actor: UnitTester
modules:
    enabled:
        - Asserts
        - \v1\Helper\Unit

测试/ functional.suite.yml

actor: FunctionalTester
modules:
    enabled:
        - Asserts
        - \v1\Helper\Functional

测试/ _bootstrap.php

<?php
define('YII_ENV', 'test');
defined('YII_DEBUG') or define('YII_DEBUG', true);

require_once(__DIR__ . '/../../../vendor/yiisoft/yii2/Yii.php');
require __DIR__ .'/../../../vendor/autoload.php';

现在我要测试的是moduels / v1 / commands / ReportController.php中的命令操作

<?php

namespace app\modules\v1\commands;

class ReportController extends Controller
{
   public function actionOverview()
   {
      echo 1;
   }
}

在我的tests \\ functional \\ commands \\ ReportControllerCest.php中,我想从commands \\ ReportController.php运行操作概述

<?php
namespace modules\v1\tests\functional\commands;

use v1\FunctionalTester;
use Yii;

class ReportControllerCest
{
    public function _before(FunctionalTester $I)
    {
    }

    public function _after(FunctionalTester $I)
    {
    }

    // tests
    public function tryToTest(FunctionalTester $I)
    {
        Yii::$app->createControllerByID('report')->run('overview');
    }
}

这是行不通的,并给我以下错误

1) ReportControllerCest: Try to test
 Test  tests\functional\commands\ReportControllerCest.php:tryToTest

  [yii\base\InvalidRouteException] Unable to resolve the request: report/overview

#1  C:\xampp\htdocs\preroll\vendor\yiisoft\yii2\base\Controller.php:189
#2  C:\xampp\htdocs\preroll\modules\v1\tests\functional\commands\ReportControllerCest.php:20
#3  modules\v1\tests\functional\commands\ReportControllerCest->tryToTest

表示不检查该命令/ReportController.php

问题。 1-这是应该如何使用各自的测试文件夹对模块进行测试吗? 如果是,那我在做什么错? 如果没有,那么正确的方法是什么? 2-如何测试命令(控制台命令)?

我将把工作留给其他有相同问题的人,以达到自己的目标。
我在“ 高级用法”中使用了代码接收环境,并为控制台和Web应用程序设置了不同的配置文件。 然后在您运行测试时使用--env codecept run --env console

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM