Skip to content

模拟

介绍

在测试 Laravel 应用程序时,您可能希望“模拟”应用程序的某些方面,以便在给定测试期间不实际执行它们。例如,在测试调度事件的控制器时,您可能希望模拟事件监听器,以便在测试期间不实际执行它们。这使您可以仅测试控制器的 HTTP 响应,而不必担心事件监听器的执行,因为事件监听器可以在自己的测试用例中进行测试。

Laravel 提供了用于模拟事件、作业和门面的助手。这些助手主要提供了一个便利层,覆盖了 Mockery,因此您不必手动进行复杂的 Mockery 方法调用。当然,您可以自由使用 Mockery 或 PHPUnit 来创建自己的模拟或间谍。

Bus Fake

作为模拟的替代方法,您可以使用 Bus 门面的 fake 方法来防止作业被调度。在使用模拟时,断言是在测试代码执行后进行的:

php
<?php

namespace Tests\Feature;

use Tests\TestCase;
use App\Jobs\ShipOrder;
use Illuminate\Support\Facades\Bus;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;

class ExampleTest extends TestCase
{
    public function testOrderShipping()
    {
        Bus::fake();

        // 执行订单发货...

        Bus::assertDispatched(ShipOrder::class, function ($job) use ($order) {
            return $job->order->id === $order->id;
        });

        // 断言作业未被调度...
        Bus::assertNotDispatched(AnotherJob::class);
    }
}

事件模拟

作为模拟的替代方法,您可以使用 Event 门面的 fake 方法来防止所有事件监听器执行。然后,您可以断言事件已被调度,甚至检查它们接收到的数据。在使用模拟时,断言是在测试代码执行后进行的:

php
<?php

namespace Tests\Feature;

use Tests\TestCase;
use App\Events\OrderShipped;
use App\Events\OrderFailedToShip;
use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;

class ExampleTest extends TestCase
{
    /**
     * 测试订单发货。
     */
    public function testOrderShipping()
    {
        Event::fake();

        // 执行订单发货...

        Event::assertDispatched(OrderShipped::class, function ($e) use ($order) {
            return $e->order->id === $order->id;
        });

        // 断言事件被调度了两次...
        Event::assertDispatched(OrderShipped::class, 2);

        // 断言事件未被调度...
        Event::assertNotDispatched(OrderFailedToShip::class);
    }
}
exclamation

调用 Event::fake() 后,不会执行任何事件监听器。因此,如果您的测试使用依赖于事件的模型工厂,例如在模型的 creating 事件期间创建 UUID,您应该在使用工厂后调用 Event::fake()

范围事件模拟

如果您只想在测试的一部分中模拟事件监听器,可以使用 fakeFor 方法:

php
<?php

namespace Tests\Feature;

use App\Order;
use Tests\TestCase;
use App\Events\OrderCreated;
use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;

class ExampleTest extends TestCase
{
    /**
     * 测试订单处理。
     */
    public function testOrderProcess()
    {
        $order = Event::fakeFor(function () {
            $order = factory(Order::class)->create();

            Event::assertDispatched(OrderCreated::class);

            return $order;
        });

        // 事件正常调度,观察者将运行...
        $order->update([...]);
    }
}

邮件模拟

您可以使用 Mail 门面的 fake 方法来防止邮件发送。然后,您可以断言 mailables 已发送给用户,甚至检查它们接收到的数据。在使用模拟时,断言是在测试代码执行后进行的:

php
<?php

namespace Tests\Feature;

use Tests\TestCase;
use App\Mail\OrderShipped;
use Illuminate\Support\Facades\Mail;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;

class ExampleTest extends TestCase
{
    public function testOrderShipping()
    {
        Mail::fake();

        // 执行订单发货...

        Mail::assertSent(OrderShipped::class, function ($mail) use ($order) {
            return $mail->order->id === $order->id;
        });

        // 断言消息已发送给指定用户...
        Mail::assertSent(OrderShipped::class, function ($mail) use ($user) {
            return $mail->hasTo($user->email) &&
                   $mail->hasCc('...') &&
                   $mail->hasBcc('...');
        });

        // 断言邮件已发送两次...
        Mail::assertSent(OrderShipped::class, 2);

        // 断言邮件未发送...
        Mail::assertNotSent(AnotherMailable::class);
    }
}

如果您将邮件排队在后台发送,应该使用 assertQueued 方法而不是 assertSent

php
Mail::assertQueued(...);
Mail::assertNotQueued(...);

通知模拟

您可以使用 Notification 门面的 fake 方法来防止通知发送。然后,您可以断言 notifications 已发送给用户,甚至检查它们接收到的数据。在使用模拟时,断言是在测试代码执行后进行的:

php
<?php

namespace Tests\Feature;

use Tests\TestCase;
use App\Notifications\OrderShipped;
use Illuminate\Support\Facades\Notification;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;

class ExampleTest extends TestCase
{
    public function testOrderShipping()
    {
        Notification::fake();

        // 执行订单发货...

        Notification::assertSentTo(
            $user,
            OrderShipped::class,
            function ($notification, $channels) use ($order) {
                return $notification->order->id === $order->id;
            }
        );

        // 断言通知已发送给指定用户...
        Notification::assertSentTo(
            [$user], OrderShipped::class
        );

        // 断言通知未发送...
        Notification::assertNotSentTo(
            [$user], AnotherNotification::class
        );
    }
}

队列模拟

作为模拟的替代方法,您可以使用 Queue 门面的 fake 方法来防止作业被排队。然后,您可以断言作业已被推送到队列,甚至检查它们接收到的数据。在使用模拟时,断言是在测试代码执行后进行的:

php
<?php

namespace Tests\Feature;

use Tests\TestCase;
use App\Jobs\ShipOrder;
use Illuminate\Support\Facades\Queue;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;

class ExampleTest extends TestCase
{
    public function testOrderShipping()
    {
        Queue::fake();

        // 执行订单发货...

        Queue::assertPushed(ShipOrder::class, function ($job) use ($order) {
            return $job->order->id === $order->id;
        });

        // 断言作业被推送到指定队列...
        Queue::assertPushedOn('queue-name', ShipOrder::class);

        // 断言作业被推送两次...
        Queue::assertPushed(ShipOrder::class, 2);

        // 断言作业未被推送...
        Queue::assertNotPushed(AnotherJob::class);
    }
}

存储模拟

Storage 门面的 fake 方法允许您轻松生成一个假磁盘,结合 UploadedFile 类的文件生成工具,大大简化了文件上传的测试。例如:

php
<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;

class ExampleTest extends TestCase
{
    public function testAvatarUpload()
    {
        Storage::fake('avatars');

        $response = $this->json('POST', '/avatar', [
            'avatar' => UploadedFile::fake()->image('avatar.jpg')
        ]);

        // 断言文件已存储...
        Storage::disk('avatars')->assertExists('avatar.jpg');

        // 断言文件不存在...
        Storage::disk('avatars')->assertMissing('missing.jpg');
    }
}
lightbulb

默认情况下,fake 方法将删除其临时目录中的所有文件。如果您希望保留这些文件,可以使用“persistentFake”方法。

门面

与传统的静态方法调用不同,门面 可以被模拟。这比传统的静态方法提供了很大的优势,并为您提供了与使用依赖注入相同的可测试性。在测试时,您可能经常希望在控制器中模拟对 Laravel 门面的调用。例如,考虑以下控制器操作:

php
<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Cache;

class UserController extends Controller
{
    /**
     * 显示应用程序的所有用户列表。
     *
     * @return Response
     */
    public function index()
    {
        $value = Cache::get('key');

        //
    }
}

我们可以使用 shouldReceive 方法模拟对 Cache 门面的调用,该方法将返回一个 Mockery 模拟实例。由于门面实际上是由 Laravel 服务容器 解析和管理的,因此它们比典型的静态类具有更高的可测试性。例如,让我们模拟对 Cache 门面的 get 方法的调用:

php
<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;

class UserControllerTest extends TestCase
{
    public function testGetIndex()
    {
        Cache::shouldReceive('get')
                    ->once()
                    ->with('key')
                    ->andReturn('value');

        $response = $this->get('/users');

        // ...
    }
}
exclamation

您不应模拟 Request 门面。相反,在运行测试时,将所需的输入传递给 HTTP 辅助方法,如 getpost。同样,不要模拟 Config 门面,而是在测试中调用 Config::set 方法。