phpunit×嘲弄|添加@runinseparateProcess @preserveglobalstate禁用,但类已经存在错误仍未解决

发布于 2025-01-31 19:35:37 字数 966 浏览 2 评论 0原文

我想模拟静态方法。

这是要测试的方法。

Use App\Model\MyModel 

class User extends Model
{
    public static function search($id)
    {
        try {
 
            User::toDo($id);
        } catch (\Exception $e) {

            MyModel::staticMethod('test', ['foo' => 'bar']);
        }
    }

我想模拟myModel并测试一次staticmethod()一次。 所以我嘲笑了以下内容。

    public function search_test($id)
    {
        $mock = \Mockery::mock('overload:'. \App\Model\MyModel::class);
        $mock->shouldReceive('staticMethod')->once();

        User::search($id);
    }

The result was

Mockery\Exception\RuntimeException: Could not load mock App\Model\MyModel , class already exists

So I added

  * @runInSeparateProcess
  * @preserveGlobalState disabled

, but still the class already exists error is not resolved. 在UserModel中没有位置新的MyModel,但是我应该在哪里检查?

先感谢您。

I would like to mock static method.

Here is the method to be tested.

Use App\Model\MyModel 

class User extends Model
{
    public static function search($id)
    {
        try {
 
            User::toDo($id);
        } catch (\Exception $e) {

            MyModel::staticMethod('test', ['foo' => 'bar']);
        }
    }

I would like to mock MyModel and test that staticMethod() is called once.
So I mocked the following.

    public function search_test($id)
    {
        $mock = \Mockery::mock('overload:'. \App\Model\MyModel::class);
        $mock->shouldReceive('staticMethod')->once();

        User::search($id);
    }

The result was

Mockery\Exception\RuntimeException: Could not load mock App\Model\MyModel , class already exists

So I added

  * @runInSeparateProcess
  * @preserveGlobalState disabled

, but still the class already exists error is not resolved.
There is no place in the UserModel where the new MyModel, but where should I check?

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文