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

Mockery fails with #39;Could not load mock ... class already exists#39; when running with --code-coverage(使用--code-coverage 运行时,嘲弄失败并显示“无法加载模拟......类已存在)
本文介绍了使用--code-coverage 运行时,嘲弄失败并显示“无法加载模拟......类已存在"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to mock a class for phpunit. Php unit fails with the error Could not load mock ... class already exists. This is the only test I'm running, so it can't be the case that the class is mocked already.

任何建议都将不胜感激.

Any suggestion would be appreciated.

这是错误情况:

namespace TestsFeature;

use TestsTestCase;

class DeactivateACSTest extends TestCase
{
    public function testDeactivateAcs()
    {
        $deviceController = Mockery::mock('overload:AppHttpControllersCloudDeviceController');
        $deviceController
            ->shouldReceive('deactivateACS')
            ->andReturn('hilfehilfehilfe');

        $devCon = new AppHttpControllersCloudDeviceController();
        $this->assertEquals('hilfehilfehilfe', $devCon->deactivateACS());
    }
}

在没有 --code-coverage 的情况下运行它:

When running it without --code-coverage it works:

[13:10:15] vagrant@homestead [~/Code/ekp] $ phpunit --filter DeactivateACS
PHPUnit 6.5.10 by Sebastian Bergmann and contributors.


 ==> TestsFeatureDeactivateACSTest              ✓

Time: 1.08 seconds, Memory: 16.00MB

OK (1 test, 3 assertions)

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

However, when running it with --code-coverage it fails:

[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.


  ==> TestsFeatureDeactivateACSTest              ⚈

Time: 5.79 seconds, Memory: 44.00MB

There was 1 error:

1) TestsFeatureDeactivateACSTest::testDeactivateAcs
MockeryExceptionRuntimeException: Could not load mock AppHttpControllersCloudDeviceController, 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

推荐答案

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

You should add these annotations before the functions that are mocking this class.

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

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

For reference you can check out the phpunit documentation.

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

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

这篇关于使用--code-coverage 运行时,嘲弄失败并显示“无法加载模拟......类已存在"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)