当使用--code-coverage运行时,模拟失败并显示“无法加载模拟...类已存在" [英] Mockery fails with 'Could not load mock ... class already exists' when running with --code-coverage

查看:156
本文介绍了当使用--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.

任何建议将不胜感激.

这是错误情况:

Here is the error case:

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的情况下运行它会起作用:

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.


 ==> Tests\Feature\DeactivateACSTest              ✓

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.


  ==> 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

推荐答案

您应该在嘲笑此类的函数之前添加这些批注.

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/当前/zh/appendixes.annotations.html#appendixes.annotations.runInSeparateProcess

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆