【问题标题】:PHPUnit error “Failed to open stream: No such file or directory”PHPUnit 错误“无法打开流:没有这样的文件或目录”
【发布时间】:2019-07-27 15:09:18
【问题描述】:

this question 有同样的问题。但是,当我将 'DIR' 添加为 this answer 时,它却出现了错误。

我的目录结构:

app\业务逻辑层\searchController.php

app\数据访问层\connect.php

app\tests\test.php

test.php

<?php
namespace Tests;
require __DIR__.'/Business Logic Layer/searchController.php';

use PHPUnit\Framework\TestCase;
use Search\SearchController as DB;

class SearchTest extends TestCase
{    
    public function testsearch()
    {
        $Ctrl=new DB;
        $result=$Ctrl->select();
        $this->assertNotNull($result);
    }
}

当我运行 phpunit 时,控制台显示错误

PHP 警告:require(F:\xamp\htdocs\AppRoot\app\tests/Business Logic Layer/searchController.php):无法打开流:F:\xamp\htdocs\AppRoot\ 中没有这样的文件或目录phonebook\tests\test.php 在第 3 行

但我的文件是 F:\xamp\htdocs\AppRoot\app\Business Logic Layer/searchController.php

我不知道怎么解决,请帮忙!

【问题讨论】:

  • 只需将 print 替换为 require ,您就可以看到它是如何解释的。
  • 错误显示 /tests/ 是您似乎正在运行它的地方。您正在运行什么命令以及在哪个文件夹中运行它。尝试下一个目录并运行它。

标签: php phpunit


【解决方案1】:

更改require __DIR__.'/Business Logic Layer/searchController.php';
require __DIR__.'\Business Logic Layer\searchController.php';

【讨论】:

  • 错误表明他们可能正在从测试文件夹中运行测试...目录在错误中显示 /tests/。
【解决方案2】:

我变了require __DIR__.'/Business Logic Layer/searchController.php';
require __DIR__.'/../Business Logic Layer/searchController.php';

它对我有用,感谢所有帮助!

【讨论】:

  • 是的,这行得通,因为您的密码(工作目录)是测试目录。从主目录运行测试也可以。很高兴你把它修好了,总是感觉很好。
猜你喜欢
  • 1970-01-01
  • 2012-03-27
  • 2015-02-25
  • 2013-12-15
  • 2014-03-02
  • 1970-01-01
  • 2016-12-20
  • 1970-01-01
相关资源
最近更新 更多