Mocking
簡介
當測試 Laravel 應用程式時,您可能希望「模擬」應用程式的某些方面,使其在給定的測試期間不會實際執行。例如,當測試一個發送事件的控制器時,您可能希望模擬事件監聽器,使其在測試期間不會實際執行。這讓您可以僅測試控制器的 HTTP 回應,而無需擔心事件監聽器的執行,因為事件監聽器可以在其自己的測試案例中進行測試。
Laravel 提供了有用的方法來模擬事件、任務和其他 facades。這些輔助方法主要在 Mockery 之上提供了一個便利層,因此您不必手動進行複雜的 Mockery 方法調用。
模擬物件
當模擬一個將透過 Laravel 的服務容器注入到您的應用程式中的物件時,您需要將您的模擬實例作為 instance
綁定綁定到容器中。這將指示容器使用您的物件模擬實例,而不是建構物件本身。
1use App\Service; 2use Mockery; 3use Mockery\MockInterface; 4 5test('something can be mocked', function () { 6 $this->instance( 7 Service::class, 8 Mockery::mock(Service::class, function (MockInterface $mock) { 9 $mock->shouldReceive('process')->once();10 })11 );12});
1use App\Service; 2use Mockery; 3use Mockery\MockInterface; 4 5public function test_something_can_be_mocked(): void 6{ 7 $this->instance( 8 Service::class, 9 Mockery::mock(Service::class, function (MockInterface $mock) {10 $mock->shouldReceive('process')->once();11 })12 );13}
為了使此操作更方便,您可以使用 Laravel 基礎測試案例類別提供的 mock
方法。例如,以下範例與上面的範例等效。
1use App\Service;2use Mockery\MockInterface;3 4$mock = $this->mock(Service::class, function (MockInterface $mock) {5 $mock->shouldReceive('process')->once();6});
當您只需要模擬物件的幾個方法時,可以使用 partialMock
方法。未模擬的方法在調用時將正常執行。
1use App\Service;2use Mockery\MockInterface;3 4$mock = $this->partialMock(Service::class, function (MockInterface $mock) {5 $mock->shouldReceive('process')->once();6});
同樣地,如果您想spy 一個物件,Laravel 的基礎測試案例類別提供了 spy
方法,作為 Mockery::spy
方法的方便包裝器。Spies 類似於 mocks;但是,spies 記錄 spy 和正在測試的程式碼之間的任何互動,讓您可以在程式碼執行後進行斷言。
1use App\Service;2 3$spy = $this->spy(Service::class);4 5// ...6 7$spy->shouldHaveReceived('process');
模擬 Facades
與傳統的靜態方法調用不同,facades(包括即時 facades)可以被模擬。與傳統的靜態方法相比,這提供了很大的優勢,並賦予您與使用傳統依賴注入時相同的可測試性。在測試時,您可能經常想要模擬對控制器之一中發生的 Laravel facade 的調用。例如,考慮以下控制器操作:
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Support\Facades\Cache; 6 7class UserController extends Controller 8{ 9 /**10 * Retrieve a list of all users of the application.11 */12 public function index(): array13 {14 $value = Cache::get('key');15 16 return [17 // ...18 ];19 }20}
我們可以使用 shouldReceive
方法來模擬對 Cache
facade 的調用,該方法將返回 Mockery mock 的實例。由於 facades 實際上是由 Laravel 服務容器解析和管理的,因此它們比典型的靜態類別具有更高的可測試性。例如,讓我們模擬對 Cache
facade 的 get
方法的調用:
1<?php 2 3use Illuminate\Support\Facades\Cache; 4 5test('get index', function () { 6 Cache::shouldReceive('get') 7 ->once() 8 ->with('key') 9 ->andReturn('value');10 11 $response = $this->get('/users');12 13 // ...14});
1<?php 2 3namespace Tests\Feature; 4 5use Illuminate\Support\Facades\Cache; 6use Tests\TestCase; 7 8class UserControllerTest extends TestCase 9{10 public function test_get_index(): void11 {12 Cache::shouldReceive('get')13 ->once()14 ->with('key')15 ->andReturn('value');16 17 $response = $this->get('/users');18 19 // ...20 }21}
您不應模擬 Request
facade。相反地,在運行測試時,將您想要的輸入傳遞到 HTTP 測試方法(例如 get
和 post
)。同樣地,不要模擬 Config
facade,而是在您的測試中調用 Config::set
方法。
Facade Spies
如果您想 spy 一個 facade,您可以對應的 facade 調用 spy
方法。Spies 類似於 mocks;但是,spies 記錄 spy 和正在測試的程式碼之間的任何互動,讓您可以在程式碼執行後進行斷言。
1<?php 2 3use Illuminate\Support\Facades\Cache; 4 5test('values are be stored in cache', function () { 6 Cache::spy(); 7 8 $response = $this->get('/'); 9 10 $response->assertStatus(200);11 12 Cache::shouldHaveReceived('put')->once()->with('name', 'Taylor', 10);13});
1use Illuminate\Support\Facades\Cache; 2 3public function test_values_are_be_stored_in_cache(): void 4{ 5 Cache::spy(); 6 7 $response = $this->get('/'); 8 9 $response->assertStatus(200);10 11 Cache::shouldHaveReceived('put')->once()->with('name', 'Taylor', 10);12}
與時間互動
在測試時,您有時可能需要修改輔助方法(例如 now
或 Illuminate\Support\Carbon::now()
)返回的時間。值得慶幸的是,Laravel 的基礎功能測試類別包含輔助方法,可讓您操作目前時間。
1test('time can be manipulated', function () { 2 // Travel into the future... 3 $this->travel(5)->milliseconds(); 4 $this->travel(5)->seconds(); 5 $this->travel(5)->minutes(); 6 $this->travel(5)->hours(); 7 $this->travel(5)->days(); 8 $this->travel(5)->weeks(); 9 $this->travel(5)->years();10 11 // Travel into the past...12 $this->travel(-5)->hours();13 14 // Travel to an explicit time...15 $this->travelTo(now()->subHours(6));16 17 // Return back to the present time...18 $this->travelBack();19});
1public function test_time_can_be_manipulated(): void 2{ 3 // Travel into the future... 4 $this->travel(5)->milliseconds(); 5 $this->travel(5)->seconds(); 6 $this->travel(5)->minutes(); 7 $this->travel(5)->hours(); 8 $this->travel(5)->days(); 9 $this->travel(5)->weeks();10 $this->travel(5)->years();11 12 // Travel into the past...13 $this->travel(-5)->hours();14 15 // Travel to an explicit time...16 $this->travelTo(now()->subHours(6));17 18 // Return back to the present time...19 $this->travelBack();20}
您也可以為各種時間旅行方法提供閉包。將在時間凍結在指定時間的情況下調用閉包。一旦閉包執行完畢,時間將恢復正常。
1$this->travel(5)->days(function () {2 // Test something five days into the future...3});4 5$this->travelTo(now()->subDays(10), function () {6 // Test something during a given moment...7});
freezeTime
方法可用於凍結目前時間。同樣地,freezeSecond
方法將凍結目前時間,但凍結在目前秒的開始。
1use Illuminate\Support\Carbon; 2 3// Freeze time and resume normal time after executing closure... 4$this->freezeTime(function (Carbon $time) { 5 // ... 6}); 7 8// Freeze time at the current second and resume normal time after executing closure... 9$this->freezeSecond(function (Carbon $time) {10 // ...11})
正如您所預期的,上面討論的所有方法主要適用於測試時間敏感的應用程式行為,例如鎖定討論論壇上的非活動貼文。
1use App\Models\Thread;2 3test('forum threads lock after one week of inactivity', function () {4 $thread = Thread::factory()->create();5 6 $this->travel(1)->week();7 8 expect($thread->isLockedByInactivity())->toBeTrue();9});
1use App\Models\Thread; 2 3public function test_forum_threads_lock_after_one_week_of_inactivity() 4{ 5 $thread = Thread::factory()->create(); 6 7 $this->travel(1)->week(); 8 9 $this->assertTrue($thread->isLockedByInactivity());10}