模拟
简介
在测试 Laravel 应用程序时,你可能希望“模拟”应用程序的某些方面,以便它们在特定测试期间不会实际执行。例如,在测试一个分发事件的控制器时,你可能希望模拟事件监听器,以便它们在测试期间不会实际执行。这允许你只测试控制器的 HTTP 响应,而无需担心事件监听器的执行,因为事件监听器可以在它们自己的测试用例中进行测试。
Laravel 为模拟事件、任务和其他门面提供了开箱即用的辅助方法。这些辅助方法主要是在 Mockery 之上提供了一层便利,这样你就不必手动进行复杂的 Mockery 方法调用。
模拟对象
当你需要通过 Laravel 的服务容器将要模拟的对象注入到你的应用程序中时,你需要将模拟实例作为 instance 绑定绑定到容器中。这将指示容器使用你的模拟对象实例,而不是构造对象本身:
use App\Service;
use Mockery;
use Mockery\MockInterface;
test('可以模拟某些东西', function () {
$this->instance(
Service::class,
Mockery::mock(Service::class, function (MockInterface $mock) {
$mock->expects('process');
})
);
});use App\Service;
use Mockery;
use Mockery\MockInterface;
public function test_something_can_be_mocked(): void
{
$this->instance(
Service::class,
Mockery::mock(Service::class, function (MockInterface $mock) {
$mock->expects('process');
})
);
}为了使这更方便,你可以使用 Laravel 的基础测试用例类提供的 mock 方法。例如,以下示例与上面的示例等效:
use App\Service;
use Mockery\MockInterface;
$mock = $this->mock(Service::class, function (MockInterface $mock) {
$mock->expects('process');
});当你只需要模拟对象的几个方法时,可以使用 partialMock 方法。未模拟的方法在被调用时将正常执行:
use App\Service;
use Mockery\MockInterface;
$mock = $this->partialMock(Service::class, function (MockInterface $mock) {
$mock->expects('process');
});同样,如果你想要监视一个对象,Laravel 的基础测试用例类提供了一个 spy 方法,作为 Mockery::spy 方法的便捷封装。间谍与模拟类似;但是,间谍会记录间谍与被测试代码之间的任何交互,允许你在代码执行后进行断言:
use App\Service;
$spy = $this->spy(Service::class);
// ...
$spy->shouldHaveReceived('process');模拟门面
与传统的静态方法调用不同,门面(包括实时门面)可以被模拟。这比传统的静态方法具有很大的优势,并且赋予了你与使用传统依赖注入时相同的可测试性。在测试时,你可能经常希望模拟在其中一个控制器中对 Laravel 门面的调用。例如,考虑以下控制器操作:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
{
/**
* 检索应用程序的所有用户列表。
*/
public function index(): array
{
$value = Cache::get('key');
return [
// ...
];
}
}我们可以使用 expects 方法模拟对 Cache 门面的调用,该方法将返回一个 Mockery 模拟实例。由于门面实际上是由 Laravel 服务容器解析和管理的,因此它们比典型的静态类具有更强的可测试性。例如,让我们模拟对 Cache 门面的 get 方法的调用:
<?php
use Illuminate\Support\Facades\Cache;
test('获取索引', function () {
Cache::expects('get')
->with('key')
->andReturn('value');
$response = $this->get('/users');
// ...
});<?php
namespace Tests\Feature;
use Illuminate\Support\Facades\Cache;
use Tests\TestCase;
class UserControllerTest extends TestCase
{
public function test_get_index(): void
{
Cache::expects('get')
->with('key')
->andReturn('value');
$response = $this->get('/users');
// ...
}
}WARNING
你不应该模拟 Request 门面。相反,在运行测试时,将你期望的输入传递给 HTTP 测试方法,例如 get 和 post。同样,不要模拟 Config 门面,而是在你的测试中调用 Config::set 方法。
门面间谍
如果你想监视一个门面,你可以在相应的门面上调用 spy 方法。间谍与模拟类似;但是,间谍会记录间谍与被测试代码之间的任何交互,允许你在代码执行后进行断言:
<?php
use Illuminate\Support\Facades\Cache;
test('值被存储在缓存中', function () {
Cache::spy();
$response = $this->get('/');
$response->assertStatus(200);
Cache::shouldHaveReceived('put')->with('name', 'Taylor', 10);
});use Illuminate\Support\Facades\Cache;
public function test_values_are_stored_in_cache(): void
{
Cache::spy();
$response = $this->get('/');
$response->assertStatus(200);
Cache::shouldHaveReceived('put')->with('name', 'Taylor', 10);
}与时间交互
在测试时,你可能偶尔需要修改由 now 或 Illuminate\Support\Carbon::now() 等辅助函数返回的时间。幸运的是,Laravel 的基础功能测试类包含了允许你操作当前时间的辅助函数:
test('时间可以被操控', function () {
// 前往未来...
$this->travel(5)->milliseconds();
$this->travel(5)->seconds();
$this->travel(5)->minutes();
$this->travel(5)->hours();
$this->travel(5)->days();
$this->travel(5)->weeks();
$this->travel(5)->years();
// 前往过去...
$this->travel(-5)->hours();
// 前往一个明确的时间...
$this->travelTo(now()->minus(hours: 6));
// 返回到当前时间...
$this->travelBack();
});public function test_time_can_be_manipulated(): void
{
// 前往未来...
$this->travel(5)->milliseconds();
$this->travel(5)->seconds();
$this->travel(5)->minutes();
$this->travel(5)->hours();
$this->travel(5)->days();
$this->travel(5)->weeks();
$this->travel(5)->years();
// 前往过去...
$this->travel(-5)->hours();
// 前往一个明确的时间...
$this->travelTo(now()->minus(hours: 6));
// 返回到当前时间...
$this->travelBack();
}你也可以向各种时间旅行方法提供一个闭包。该闭包将在时间冻结在指定时间时被调用。一旦闭包执行完毕,时间将恢复正常:
$this->travel(5)->days(function () {
// 测试未来五天内的某些东西...
});
$this->travelTo(now()->minus(days: 10), function () {
// 在给定时刻测试某些东西...
});freezeTime 方法可用于冻结当前时间。同样,freezeSecond 方法将冻结当前时间,但在当前秒的开始处:
use Illuminate\Support\Carbon;
// 冻结时间,并在执行闭包后恢复正常时间...
$this->freezeTime(function (Carbon $time) {
// ...
});
// 冻结在当前秒,并在执行闭包后恢复正常时间...
$this->freezeSecond(function (Carbon $time) {
// ...
})正如你所期望的,上面讨论的所有方法主要用于测试对时间敏感的应用程序行为,例如在讨论论坛上锁定不活跃的帖子:
use App\Models\Thread;
test('论坛帖子在不活跃一周后被锁定', function () {
$thread = Thread::factory()->create();
$this->travel(1)->week();
expect($thread->isLockedByInactivity())->toBeTrue();
});use App\Models\Thread;
public function test_forum_threads_lock_after_one_week_of_inactivity()
{
$thread = Thread::factory()->create();
$this->travel(1)->week();
$this->assertTrue($thread->isLockedByInactivity());
}