Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel Mail::assertQueued BadMethodCallException_Php_Laravel_Unit Testing_Phpunit - Fatal编程技术网

Php Laravel Mail::assertQueued BadMethodCallException

Php Laravel Mail::assertQueued BadMethodCallException,php,laravel,unit-testing,phpunit,Php,Laravel,Unit Testing,Phpunit,我正在尝试编写一个laravelphpunit测试,检查在创建用户后邮件是否已排队 <?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\Supp

我正在尝试编写一个laravelphpunit测试,检查在创建用户后邮件是否已排队

<?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测试\Unit\User\UserUnitTest::testEmailSentAfterUserCreated BadMethodCallException:Method\Mail\Mailer::assertQueued 不存在

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

邮件类已经包含在内,我确信参数是正确的,但是我不确定为什么会出现这个错误

没有排队的断言,它有一个assertSentTo。下面是一个它应该如何显示的示例。如果通知可以排队,我认为您可以使用Queue::false来实现这一点

Notification::assertSentTo(
    [$user], UserCreated::class
);

如果要在Mail::assertQueued上断言,请使用Mail::fake。我也面临同样的问题。我忘了在那个特定的测试用例中添加Mail::fake。

我的回答是否有帮助,或者您面临的问题是什么?如果您在后台将邮件排队等待传递,您应该使用assertQueued方法而不是assertSent: