Laravel 8中的单元测试事件抛出EventFake::\uu construct()必须实现接口Illumb\Contracts\Events\Dispatcher,给定null

Laravel 8中的单元测试事件抛出EventFake::\uu construct()必须实现接口Illumb\Contracts\Events\Dispatcher,给定null,laravel,Laravel,我试图用单元测试来伪造一个事件,所以没有数据库,没有应用程序,只有纯类 我正在遵循Laravel8的文档,我的当前版本,但是我仍然得到了错误 1) Tests\Unit\HandShake\ConfirmApplicationHandShakeActionTest::BasedOnAnExistentAppIConfirmTheHandShake TypeError: Argument 1 passed to Illuminate\Support\Testing\Fakes\EventFake:

我试图用单元测试来伪造一个事件,所以没有数据库,没有应用程序,只有纯类

我正在遵循Laravel8的文档,我的当前版本,但是我仍然得到了错误

1) Tests\Unit\HandShake\ConfirmApplicationHandShakeActionTest::BasedOnAnExistentAppIConfirmTheHandShake
TypeError: Argument 1 passed to Illuminate\Support\Testing\Fakes\EventFake::__construct() must implement interface Illuminate\Contracts\Events\Dispatcher, null given, called in /Users/pablo/Workspace/xxxx/vendor/laravel/framework/src/Illuminate/Support/Facades/Event.php on line 38
正如您所看到的,在请求一个实现接口分派的对象时,我的事件具有trait Dispatch,但无论如何这里它表示我正在传递
null

我的测试代码如下

<?php

namespace Tests\Unit\HandShake;

use App\Actions\ConfirmApplicationHandShakeAction;
use App\Events\HandShakeReceivedEvent;
use App\Exceptions\NotFoundApplicationException;
use App\Models\Application;
use Illuminate\Support\Facades\Event;
use Mockery\MockInterface;
use Mockery;

use PHPUnit\Framework\TestCase;

class ConfirmApplicationHandShakeActionTest extends TestCase
{

    /**
     *
     * @test
     * @throws NotFoundApplicationException
     */
    public function BasedOnAnExistentAppIConfirmTheHandShake()
    {

        $appName = 'testDummy';

        Event::fake([HandShakeReceivedEvent::class]);

        $applicationModel = Mockery::mock(Application::class, function (MockInterface $mock) {
            $mock->shouldReceive('exists')
                ->andReturn(true)
                ->once();
        });
        $confirmation = new ConfirmApplicationHandShakeAction($applicationModel);
        $confirmation->execute($appName);

问题已解决

当您使用artisan创建测试文件时,出于某些原因,他们使用的
PHPUnit\Framework\TestCase
是PHPUnit中的文件,但他们应该使用Laravel
Tests\TestCase

使用
tests\TestCase
扩展测试将解决问题

    <?php
declare(strict_types=1);

namespace App\Actions;

use App\Events\HandShakeReceivedEvent;

use App\Exceptions\NotFoundApplicationException;
use App\Models\Application;


/**
 * Class ConfirmApplicationHandShakeAction
 * @package App\Actions
 */
class ConfirmApplicationHandShakeAction
{
    /**
     * @var Application
     */
    private $application;

    /**
     * ConfirmApplicationHandShakeAction constructor.
     * @param Application $application
     */
    public function __construct(Application $application)
    {
        $this->application = $application;
    }

    /**
     * @param string $appName
     * @throws NotFoundApplicationException
     */
    public function execute(string $appName)
    {

        if ( ! $this->application->exists($appName)) {
            throw new NotFoundApplicationException('The Application do not exists or is not active');
        }

        HandShakeReceivedEvent::dispatch($appName);
    }
}
    <?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class HandShakeReceivedEvent
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }
}