模拟
介绍
在测试 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->shouldReceive('process')->once();
})
);
});
use App\Service;
use Mockery;
use Mockery\MockInterface;
public function test_某些内容可以被模拟(): void
{
$this->instance(
Service::class,
Mockery::mock(Service::class, function (MockInterface $mock) {
$mock->shouldReceive('process')->once();
})
);
}
为了使这更方便,您可以使用 Laravel 基础测试类提供的 mock
方法。例如,以下示例与上面的示例等效:
use App\Service;
use Mockery\MockInterface;
$mock = $this->mock(Service::class, function (MockInterface $mock) {
$mock->shouldReceive('process')->once();
});
您可以使用 partialMock
方法,当您只需要模拟对象的少数方法时。未被模拟的方法在调用时将正常执行:
use App\Service;
use Mockery\MockInterface;
$mock = $this->partialMock(Service::class, function (MockInterface $mock) {
$mock->shouldReceive('process')->once();
});
类似地,如果您想要 间谍 一个对象,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 [
// ...
];
}
}
我们可以通过使用 shouldReceive
方法来模拟对 Cache
外观的调用,这将返回一个 Mockery 模拟的实例。由于外观实际上是由 Laravel 服务容器 解析和管理的,因此它们比典型的静态类具有更高的可测试性。例如,让我们模拟对 Cache
外观的 get
方法的调用:
<?php
use Illuminate\Support\Facades\Cache;
test('获取索引', function () {
Cache::shouldReceive('get')
->once()
->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_获取索引(): void
{
Cache::shouldReceive('get')
->once()
->with('key')
->andReturn('value');
$response = $this->get('/users');
// ...
}
}
您不应模拟 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')->once()->with('name', 'Taylor', 10);
});
use Illuminate\Support\Facades\Cache;
public function test_值被存储在缓存中(): void
{
Cache::spy();
$response = $this->get('/');
$response->assertStatus(200);
Cache::shouldHaveReceived('put')->once()->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()->subHours(6));
// 返回到当前时间...
$this->travelBack();
});
public function test_时间可以被操纵(): 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()->subHours(6));
// 返回到当前时间...
$this->travelBack();
}
您还可以向各种时间旅行方法提供一个闭包。闭包将在指定时间冻结时调用。闭包执行后,时间将恢复正常:
$this->travel(5)->days(function () {
// 测试某些内容在未来五天内...
});
$this->travelTo(now()->subDays(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_论坛主题在一周不活跃后锁定()
{
$thread = Thread::factory()->create();
$this->travel(1)->week();
$this->assertTrue($thread->isLockedByInactivity());
}