微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 当使用–code-coverage运行时,Mockery失败并且“无法加载模拟…类已经存在”

我正在尝试为PHPunit模拟一个类. PHP单元失败并显示错误无法加载模拟…类已存在.这是我正在运行的唯一测试,因此不能认为该类已被嘲笑.

任何建议将不胜感激.

这是错误情况:
    

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

解决方法:

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

/**
 * @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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐