嘲弄失败,“无法加载模拟...类已经存在”与--code覆盖运行时

问题描述 投票:3回答: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
php unit-testing phpunit code-coverage php-7.0
1个回答
8
投票

你应该被嘲讽这个类的功能之前添加这些注释。

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


0
投票

我遇到了同样的问题,固定这样的: 1.在我的单元测试的另一个测试(不嘲弄测试)中具有上有我在嘲笑类的PHP文件require_once。删除了该行。 2.增加processIsolation =“真”,在测试套件

© www.soinside.com 2019 - 2024. All rights reserved.