Nes*_*k27 3 php phpunit unit-testing autoload phpstorm
我是 PHPUnit 和单元测试的新手,所以我通过 Composer 安装 PHPUnit 和 phar,一切都很顺利,直到我尝试开始简单的测试。我正在使用 PhpStorm,可以看到所有类都是自动加载的,但是当我尝试开始测试时出现错误:
Fatal error: Class 'PharIo\Manifest\Simple' not found in C:\xampp\htdocs\mydocs\
我不明白为什么他要在 PHPUnit 存在的文件夹上方寻找它?
我试图在composer.json 中配置自动加载部分并检查phpunit.xml 中的设置,但没有任何效果。
添加:
我必须在没有 PharIO 的情况下重新安装 PHPUnit,所以现在我有了一点进展,现在我遇到了一种情况,如果我使用被测试类的名称创建 require_once 行,我就可以测试我的类。看起来像:
require_once '../src/Simple.php';
class SimpleTest extends PHPUnit_Framework_TestCase
{
    public function testAdd() {
        $sum = new Simple();
        $this->assertEquals(5, $sum->add(2, 3));
    }
}
所以我的简单课程是:
class Simple {
public function add($a, $b) {
    return (int) $a + (int) $b;
}
}
但是,当然,我想使用命名空间。我尝试根据这个问题进行一些更改:使用 Composer 和 autoload.php 在 PHPUnit 中自动加载类(我什至尝试使用该存储库进行测试,但错误仍然存在),但对我来说没有任何作用。我尝试像这样编辑composer.json中的自动加载部分
"autoload": {
    "psr-4": {
        "app\\": "src/"
    }
},
但错误仍然存在,也就是说autoload看不到它。我使用相同的设置创建 phpunit.xml 和 phpunit.dist.xml
<phpunit
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/3.7/phpunit.xsd"
        backupGlobals="true"
        backupStaticAttributes="false"
        colors="true"
        convertErrorsToExceptions="true"
        convertNoticesToExceptions="true"
        convertWarningsToExceptions="true"
        processIsolation="false"
        stopOnFailure="false"
        syntaxCheck="false"
        bootstrap="./tests/bootstrap.php">
        <testsuites>
                <testsuite name="The project's test suite">
                        <directory>./tests</directory>
                </testsuite>
        </testsuites>
</phpunit>
我也做了测试/bootstrap.php
require_once '../vendor/autoload.php';
我知道这是一个老问题,但也许你需要这样做
composer dump-autoload供作曲家生成类映射。
我浪费了 30 分钟试图理解为什么 PHPUnit 给了我:
Cannot stub or mock class or interface XXX because it doesn't exists
| 归档时间: | 
 | 
| 查看次数: | 10739 次 | 
| 最近记录: |