Laravel 10中的WithoutEvents trait引发了无法禁用事件的错误。

huangapple go评论66阅读模式
英文:

Laravel 10 WithoutEvents trait throws unable to disable events error

问题

我的应用程序在很大程度上依赖于事件。然而,我的许多测试不需要事件,因此我依赖于\Illuminate\Foundation\Testing\WithoutEvents特性来禁用它们。在从 Laravel 9 迁移到 Laravel 10 时,我的测试抛出了"无法禁用事件。未使用 ApplicationTrait"的错误。

我创建了一个全新的 Laravel 10 安装,并将 WithoutEvents 特性添加到示例功能测试中:

<?php

namespace Tests\Feature;

// use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutEvents;
use Tests\TestCase;

class ExampleTest extends TestCase
{
    use WithoutEvents;

    /**
     * 一个基本的测试示例。
     */
    public function test_the_application_returns_a_successful_response(): void
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
}

使用vendor/bin/phunit运行测试返回:

时间:00:00.138,内存:22.00 MB

发生了 1 个错误:

1) Tests\Feature\ExampleTest::test_the_application_returns_a_successful_response
异常:无法禁用事件。未使用 ApplicationTrait。

如果我在一个全新的 Laravel 9 安装中执行相同的操作,测试就会正常运行。有没有什么想法这里发生了什么?值得一提的是,我使用的是 PHP 8.1.11 和 Composer 2.5.4。

英文:

My application depends heavily on events. However, many of my tests don't require events, so I rely on the \Illuminate\Foundation\Testing\WithoutEvents trait to disable them. In migrating to Laravel 10 from 9, my tests are throwing "Unable to disable events. ApplicationTrait not used."

I created a fresh Laravel 10 install and added the WithoutEvents trait to the example feature test:

&lt;?php

namespace Tests\Feature;

// use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutEvents;
use Tests\TestCase;

class ExampleTest extends TestCase
{
    use WithoutEvents;

    /**
     * A basic test example.
     */
    public function test_the_application_returns_a_successful_response(): void
    {
        $response = $this-&gt;get(&#39;/&#39;);

        $response-&gt;assertStatus(200);
    }
}

Running the tests with vendor/bin/phunit returns:

Time: 00:00.138, Memory: 22.00 MB

There was 1 error:

1) Tests\Feature\ExampleTest::test_the_application_returns_a_successful_response
Exception: Unable to disable events. ApplicationTrait not used.

If I do the same in a fresh Laravel 9 install, the test runs fine. Any idea what's going on here? FWIW, I'm on PHP 8.1.11 and composer 2.5.4.

答案1

得分: 4

它引发异常,因为正如Aless55在问题的评论中指出的,包括protected function withoutEvents()的MocksApplicationServices特性已从\Illuminate\Foundation\Testing\TestCase中删除。当WithoutEvents特性找不到这个方法时,它会引发错误。

指南并不清楚,如果您使用WithoutEvents(它尚未从框架中删除),您需要转而在setUp()或个别测试中使用Event::fake()。请参阅升级指南:https://laravel.com/docs/10.x/upgrade#service-mocking

英文:

It throws the exception because as Aless55 points out in the comments to the question, the MocksApplicationServices trait which included protected function withoutEvents() has been removed from \Illuminate\Foundation\Testing\TestCase. When the WithoutEvents trait can't find this method, it throws the error.

The guide is not clear that if you use WithoutEvents (which has not been removed from the framework), you need to move to using Event::fake() in setUp() or in your individual tests. See the upgrade guide: https://laravel.com/docs/10.x/upgrade#service-mocking

huangapple
  • 本文由 发表于 2023年2月24日 02:32:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548936.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定