Laravel Mail :: assertQueued BadMethodCallException

问题描述 投票:0回答:1

我正在尝试编写Laravel PHPUnit测试,以检查在创建用户之后是否已将邮件排队。

<?php

namespace Tests\Unit\User;

use App\User;
use Tests\TestCase;
use App\Notifications\UserCreated;
use Illuminate\Support\Facades\Mail;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Notification;
use Illuminate\Foundation\Testing\RefreshDatabase;

class UserUnitTest extends TestCase
{
    use RefreshDatabase;

    /**
     * check if a user was created in database
     *
     * @return void
     */
    public function testUserCreate()
    {
        $user = factory(User::class)->create();

        $this->assertDatabaseHas('users', [
            'email'             => $user->email,
            'active'            => 0,
            'activation_token'  => $user->activation_token,
            'deleted_at'        => NULL
        ]);
    }

    /**
     * check if email was sent after user was created in database
     *
     * @return void
     */
    public function testEmailSentAfterUserCreated()
    {
        Notification::fake();

        // Assert that no notifications were sent...
        Notification::assertNothingSent();

        $user = factory(User::class)->create();

        // Assert a notification was sent to the given users...
        Mail::assertQueued(UserCreated::class, 1);
    }
}

当我运行此测试testEmailSentAfterUserCreated时,它将引发以下异常。

有1个错误:

1)Tests \ Unit \ User \ UserUnitTest :: testEmailSentAfterUserCreatedBadMethodCallException:方法照亮\ Mail \ Mailer :: assertQueued不存在。

/ home / vagrant / Projects / endiro / vendor / laravel / framework / src / Illuminate / Support / Traits / Macroable.php:103/home/vagrant/Projects/endiro/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:245/home/vagrant/Projects/endiro/tests/Unit/User/UserUnitTest.php:49

Mail类已包含在内,我确定参数正确,但是不确定为什么会出现此错误。

php laravel unit-testing phpunit
1个回答
0
投票

Notifications没有断言排队,它有一个assertSentTo()。因此它是一个外观示例。如果通知可以排队,我认为您可以使用Queue :: fake()实现此目的。

Notification::assertSentTo(
    [$user], UserCreated::class
);
© www.soinside.com 2019 - 2024. All rights reserved.