首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用--code-coverage运行时,Mockery失败,并显示“无法加载mock ...类已存在”

使用--code-coverage运行时,Mockery失败,并显示“无法加载mock ...类已存在”
EN

Stack Overflow用户
提问于 2018-08-06 21:01:06
回答 2查看 9.9K关注 0票数 20

Php单元失败,出现错误Could not load mock ... class already exists。这是我正在运行的唯一一个测试,所以它不可能是类已经被模仿的情况。

任何建议都将不胜感激。

下面是错误案例:

代码语言:javascript
运行
复制
namespace Tests\Feature;

use Tests\TestCase;

class DeactivateACSTest extends TestCase
{
    public function testDeactivateAcs()
    {
        $deviceController = \Mockery::mock('overload:App\Http\Controllers\Cloud\DeviceController');
        $deviceController
            ->shouldReceive('deactivateACS')
            ->andReturn('hilfehilfehilfe');

        $devCon = new \App\Http\Controllers\Cloud\DeviceController();
        $this->assertEquals('hilfehilfehilfe', $devCon->deactivateACS());
    }
}

在不使用--code-coverage的情况下运行时,它可以工作:

代码语言:javascript
运行
复制
[13:10:15] vagrant@homestead [~/Code/ekp] $ phpunit --filter DeactivateACS
PHPUnit 6.5.10 by Sebastian Bergmann and contributors.


 ==> Tests\Feature\DeactivateACSTest              ✓

Time: 1.08 seconds, Memory: 16.00MB

OK (1 test, 3 assertions)

但是,当使用--code-coverage运行它时,它会失败:

代码语言:javascript
运行
复制
[13:10:23] vagrant@homestead [~/Code/ekp] $ phpunit --coverage-html coverage --coverage-text=code_coverage.txt --filter DeactivateACSTest
PHPUnit 6.5.10 by Sebastian Bergmann and contributors.


  ==> Tests\Feature\DeactivateACSTest              ⚈

Time: 5.79 seconds, Memory: 44.00MB

There was 1 error:

1) Tests\Feature\DeactivateACSTest::testDeactivateAcs
Mockery\Exception\RuntimeException: Could not load mock \App\Http\Controllers\Cloud\DeviceController, class already exists

/home/vagrant/Code/ekp/vendor/mockery/mockery/library/Mockery/Container.php:220
/home/vagrant/Code/ekp/vendor/mockery/mockery/library/Mockery.php:116
/home/vagrant/Code/ekp/tests/Feature/DeactivateACSTest.php:11

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

Generating code coverage report in HTML format ... done
EN

回答 2

Stack Overflow用户

发布于 2018-11-13 01:11:13

您应该在模拟该类的函数之前添加这些注释。

代码语言:javascript
运行
复制
/**
 * @runInSeparateProcess
 * @preserveGlobalState disabled
 */

作为参考,您可以查看phpunit文档。

https://phpunit.de/manual/current/en/appendixes.annotations.html#appendixes.annotations.runInSeparateProcess

https://phpunit.de/manual/current/en/appendixes.annotations.html#appendixes.annotations.preserveGlobalState

票数 32
EN

Stack Overflow用户

发布于 2019-08-09 00:57:19

我遇到了同样的问题,修复方法如下:

在我的单元测试(不是模拟测试)中有另一个测试(不是模拟测试),它对包含我正在模拟的类的文件进行了require_once。我已经删除了这一行,我已经在测试套件中添加了processIsolation="true"

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51708289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档