热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《当使用--code-coverage运行时,模拟失败并显示“无法加载模拟类已存在”》经验,为你挑选了1个好方法。

我正在尝试模拟phpunit的类。php单元因错误而失败Could not load mock ... class already exists。这是我正在运行的唯一测试,因此不可能已经模拟了该类。

任何建议,将不胜感激。

这是错误情况:

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它的情况下运行时:

[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它一起运行时失败:

[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

小智.. 12

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

/**
 * @runInSeparateProcess
 * @preserveGlobalState disabled
 */

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

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

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



1> 小智..:

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

/**
 * @runInSeparateProcess
 * @preserveGlobalState disabled
 */

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

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

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


推荐阅读
author-avatar
Because_of_you龙
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有