测试模拟器

    Laravel 针对事件、任务和 Facades 的模拟,提供了开箱即用的辅助函数。这些函数基于 Mocker 封装而成,使用非常方便,无需手动调用复杂的 Mockery 函数。当然你也可以使用 Mockery 或者使用 PHPUnit 创建自己的模拟器。

    任务模拟

    你可以使用 Facade 的 fake 方法来模拟事件监听,测试的时候并不会真正触发事件监听器。然后你就可以测试断言事件运行了,甚至可以检查他们接收的数据。使用 fake 的时候,断言一般出现在测试代码的后面:

    1. <?php
    2. namespace Tests\Feature;
    3. use Tests\TestCase;
    4. use App\Events\OrderShipped;
    5. use App\Events\OrderFailedToShip;
    6. use Illuminate\Support\Facades\Event;
    7. use Illuminate\Foundation\Testing\RefreshDatabase;
    8. use Illuminate\Foundation\Testing\WithoutMiddleware;
    9. class ExampleTest extends TestCase
    10. {
    11. /**
    12. * 测试订单发货
    13. */
    14. public function testOrderShipping()
    15. {
    16. Event::fake();
    17. // 处理订单发货...
    18. Event::assertDispatched(OrderShipped::class, function ($e) use ($order) {
    19. return $e->order->id === $order->id;
    20. });
    21. // 断言事件执行两次...
    22. Event::assertDispatched(OrderShipped::class, 2);
    23. // 断言事件并没有被执行...
    24. Event::assertNotDispatched(OrderFailedToShip::class);
    25. }
    26. }

    邮件模拟

    1. <?php
    2. namespace Tests\Feature;
    3. use Tests\TestCase;
    4. use App\Mail\OrderShipped;
    5. use Illuminate\Support\Facades\Mail;
    6. use Illuminate\Foundation\Testing\RefreshDatabase;
    7. use Illuminate\Foundation\Testing\WithoutMiddleware;
    8. class ExampleTest extends TestCase
    9. {
    10. public function testOrderShipping()
    11. {
    12. Mail::fake();
    13. // 执行订单发送...
    14. Mail::assertSent(OrderShipped::class, function ($mail) use ($order) {
    15. return $mail->order->id === $order->id;
    16. });
    17. // 断言一条发送给用户的消息...
    18. Mail::assertSent(OrderShipped::class, function ($mail) use ($user) {
    19. return $mail->hasTo($user->email) &&
    20. $mail->hasCc('...') &&
    21. $mail->hasBcc('...');
    22. });
    23. // 断言邮件被发送两次...
    24. Mail::assertSent(OrderShipped::class, 2);
    25. // 断言没有发送邮件...
    26. Mail::assertNotSent(AnotherMailable::class);
    27. }
    28. }

    如果你用后台任务执行邮件发送队列,你应该是用 assertQueued 代替 assertSent

    你可以使用 Notification Facade 的 fake 方法来模拟通知的发送,测试时并不会真的发出通知。然后你可以断言 发送给了用户,甚至可以检查他们收到的内容。使用 fakes 时,断言一般放在测试代码后面:

    1. <?php
    2. namespace Tests\Feature;
    3. use Tests\TestCase;
    4. use App\Notifications\OrderShipped;
    5. use Illuminate\Support\Facades\Notification;
    6. use Illuminate\Foundation\Testing\RefreshDatabase;
    7. use Illuminate\Foundation\Testing\WithoutMiddleware;
    8. class ExampleTest extends TestCase
    9. {
    10. public function testOrderShipping()
    11. {
    12. Notification::fake();
    13. // 处理订单发货...
    14. Notification::assertSentTo(
    15. $user,
    16. OrderShipped::class,
    17. function ($notification, $channels) use ($order) {
    18. return $notification->order->id === $order->id;
    19. }
    20. );
    21. // 断言通知已经发送给了指定用户...
    22. Notification::assertSentTo(
    23. [$user], OrderShipped::class
    24. );
    25. // 断言通知没有发送...
    26. Notification::assertNotSentTo(
    27. [$user], AnotherNotification::class
    28. );
    29. }
    30. }

    队列模拟

    你可以使用 Queue Facade 的 fake 方法来模拟任务队列,测试的时候并不会真的把任务放入队列。然后你可以断言任务被放入了队列,甚至可以检查他们收到的内容。使用 fakes 时,断言一般放在测试代码的后面:

    1. <?php
    2. namespace Tests\Feature;
    3. use Tests\TestCase;
    4. use App\Jobs\ShipOrder;
    5. use Illuminate\Support\Facades\Queue;
    6. use Illuminate\Foundation\Testing\RefreshDatabase;
    7. use Illuminate\Foundation\Testing\WithoutMiddleware;
    8. class ExampleTest extends TestCase
    9. public function testOrderShipping()
    10. {
    11. Queue::fake();
    12. // 处理订单发货...
    13. Queue::assertPushed(ShipOrder::class, function ($job) use ($order) {
    14. return $job->order->id === $order->id;
    15. // 断言任务进入了指定队列...
    16. Queue::assertPushedOn('queue-name', ShipOrder::class);
    17. // 断言任务进入2次...
    18. Queue::assertPushed(ShipOrder::class, 2);
    19. // 断言任务没有进入队列...
    20. Queue::assertNotPushed(AnotherJob::class);
    21. }
    22. }

    你可以使用 Storage Facade 的 fake 方法,轻松的生成一个模拟磁盘,结合 UploadedFile 类的文件生成工具,极大的简化了文件上传测试。例如:

    Facades

    与传统的静态方法调用,不同 facades 也可以被模拟。相对静态函数调用来说这是一个巨大的优势,即时你在使用依赖注入,测试时依然非常方便。在测试中,你可能想在控制器中模拟对 Laravel Facade 的调用。比如下面控制器中的行为:

    1. <?php
    2. namespace App\Http\Controllers;
    3. use Illuminate\Support\Facades\Cache;
    4. class UserController extends Controller
    5. {
    6. /**
    7. * 显示应用里所有用户
    8. *
    9. * @return Response
    10. */
    11. public function index()
    12. {
    13. $value = Cache::get('key');
    14. //
    15. }
    16. }

    我们可以通过 shouldReceive 方法来模拟 Cache Facade,此函数会返回一个 实例。由于 Facade 的调用实际是由 Laravel 的 服务容器 管理的,所以 Facade 能比传统的静态类表现出更好的测试便利性。下面,让我们模拟一下 Cache Facade 的 get 方法:

    1. <?php
    2. namespace Tests\Feature;
    3. use Tests\TestCase;
    4. use Illuminate\Support\Facades\Cache;
    5. use Illuminate\Foundation\Testing\RefreshDatabase;
    6. use Illuminate\Foundation\Testing\WithoutMiddleware;
    7. class UserControllerTest extends TestCase
    8. {
    9. public function testGetIndex()
    10. {
    11. Cache::shouldReceive('get')
    12. ->once()
    13. ->with('key')
    14. ->andReturn('value');
    15. $response = $this->get('/users');
    16. // ...
    17. }

    本文章首发在 网站上。