代码之家  ›  专栏  ›  技术社区  ›  Sven Hakvoort

扩展抽象测试类时找不到测试类

  •  0
  • Sven Hakvoort  · 技术社区  · 7 年前

    我目前正在使用lumen框架(v5.6)并为我的代码编写单元测试。

    我有一个基本类 TestCase :

    namespace Tests;
    
    $_SERVER["http_proxy"] = "";
    
    abstract class TestCase extends \Laravel\Lumen\Testing\TestCase
    {
        /**
         * Creates the application.
         *
         * @return \Laravel\Lumen\Application
         */
        public function createApplication()
        {
            return require __DIR__.'/../bootstrap/app.php';
        }
    }
    

    namespace Tests\App\IO;
    
    use App\io\PageDataParser;
    use App\Models\AdvancedArray;
    use App\Services\PageService;
    use Mockery;
    use Tests\TestCase;
    
    abstract class ParsePageDataTestCase extends TestCase
    {
    
        // Test logic here, but not relevant for the question
    
    }
    

    最后我在实际测试中使用了这个抽象类:

    namespace Tests\App\IO;
    
    use App\io\JsonPageDataParser;
    use App\Models\AdvancedArray;
    
    class JsonParsePageDataTestCaseTest extends ParsePageDataTestCase
    {
    
       // Test are here, but not relevant for the question
    }
    

    JsonParsePageDataTestCaseTest 我得到以下错误:

    PHP致命错误:在第15行的\Tests\app\io\JsonParsePageDataTest.PHP中找不到类“Tests\ParsePageDataTestCase”

    and verified that my has an entry which specifies a classmap to 'tests/

    我使用phpunit.xml执行测试,它加载 bootstrap/app.php ,但我仍然得到这个错误。

    phpunit.xml :

    <?xml version="1.0" encoding="UTF-8"?>
    <phpunit backupGlobals="false"
             backupStaticAttributes="false"
             bootstrap="bootstrap/app.php"
             colors="true"
             convertErrorsToExceptions="true"
             convertNoticesToExceptions="true"
             convertWarningsToExceptions="true"
             processIsolation="false"
             stopOnFailure="false">
        <testsuites>
            <testsuite name="Application test Suite">
                <directory suffix="Test.php">./tests</directory>
            </testsuite>
        </testsuites>
        <filter>
            <whitelist processUncoveredFilesFromWhitelist="true">
                <directory suffix=".php">./app</directory>
            </whitelist>
        </filter>
        <php>
               <!-- ENV variables go here -->
    
        </php>
    </phpunit> 
    

    最后我的 composer.json :

    {
        "name": "laravel/lumen",
        "description": "The Laravel Lumen Framework.",
        "keywords": ["framework", "laravel", "lumen"],
        "license": "MIT",
        "type": "project",
        "require": {
            "php": ">=7.1.3",
            "laravel/lumen-framework": "5.6.*",
            "vlucas/phpdotenv": "~2.2",
            "willdurand/hateoas": "~2.1",
            "guzzlehttp/guzzle": "~6.0",
            "ext-json": "*",
            "vinelab/neoeloquent": "^1.4.6",
            "jenssegers/mongodb": "3.4.*",
            "predis/predis": "~1.0"
        },
        "require-dev": {
            "fzaninotto/faker": "~1.4",
            "phpunit/phpunit": "~7.0",
            "mockery/mockery": "~1.0",
            "barryvdh/laravel-ide-helper": "~2.5"
        },
        "autoload": {
            "classmap": [
                "database/seeds",
                "database/factories"
            ],
            "psr-4": {
                "App\\": "app/"
            }
        },
        "autoload-dev": {
            "classmap": [
                "tests/"
            ]
        },
        "scripts": {
            "post-root-package-install": [
                "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
            ]
        },
        "config": {
            "preferred-install": "dist",
            "sort-packages": true,
            "optimize-autoloader": true
        },
        "minimum-stability": "dev",
        "prefer-stable": true
    }
    

    提前谢谢!

    1 回复  |  直到 7 年前
        1
  •  4
  •   Sven Hakvoort    7 年前

    这是内腔安装的问题。

    {
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    ...
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    ...
    "minimum-stability": "dev",
    "prefer-stable": true
    

    }

    但在内腔安装上,我们可以看到以下情况:

    {
    "name": "laravel/lumen",
    "description": "The Laravel Lumen Framework.",
    "keywords": ["framework", "laravel", "lumen"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=7.1.3",
        "laravel/lumen-framework": "5.7.*",
        "vlucas/phpdotenv": "~2.2"
    },
    "require-dev": {
        "fzaninotto/faker": "~1.4",
        "phpunit/phpunit": "~7.0",
        "mockery/mockery": "~1.0"
    },
    "autoload": {
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "classmap": [
            "tests/"
        ]
    },
    "scripts": {
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    },
    "minimum-stability": "dev",
    "prefer-stable": true
    }
    

    "autoload-dev": {
        "classmap": [
            "tests/"
        ],
        "psr-4": {
            "Tests\\": "tests/"
        }
    }
    
    推荐文章