Mar*_*sen 0 php phpunit unit-testing laravel
我正在尝试编写一个 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);
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行此测试testEmailSentAfterUserCreated时,它会引发以下异常。
有 1 个错误:
1) Tests\Unit\User\UserUnitTest::testEmailSentAfterUserCreated BadMethodCallException:方法 Illuminate\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 类已包含在内,我确信参数是正确的,但我不确定为什么会收到此错误。
如果您想在 Mail::assertQueued() 上进行断言,请使用 Mail::fake()。我面临着同样的问题。我忘记在该特定测试用例中添加 Mail::fake() 。