您好,登錄后才能下訂單哦!
什么是callable-fake?怎么使用?相信很多人對(duì)php中的callable-fake不了解,小編給大家總結(jié)了以下內(nèi)容。如下資料是關(guān)于callable-fake的內(nèi)容。
Callable fake 是 Tim Macdonald 的一個(gè) PHP 測(cè)試實(shí)用程序,它 “允許您偽造、捕獲和斷言對(duì)可調(diào)用 / 閉包的調(diào)用”。在某些情況下,此包可以幫助在測(cè)試中允許開(kāi)發(fā)人員傳遞一個(gè) callable。
它有一個(gè)受 Laravel 虛構(gòu)啟發(fā)的 API,如下所示:
// Before, you might collect callables to assert later... public function testEachLoopsOverAllDependencies(): void { // arrange $received = []; $expected = factory(Dependency::class)->times(2)->create(); $repo = $this->app[DependencyRepository::class]; // act $repo->each(function (Dependency $dependency) use (&$received): void { $received[] = $dependency; }); // assert $this->assertCount(2, $received); $this->assertTrue($expected[0]->is($received[0])); $this->assertTrue($expected[1]->is($received[1])); }
使用此軟件包,您可以使用類似以下內(nèi)容的內(nèi)容:
public function testEachLoopsOverAllDependencies(): void { // arrange $callable = new CallableFake(); $expected = factory(Dependency::class)->times(2)->create(); $repo = $this->app[DependencyRepository::class]; // act $repo->each($callable); // assert $callable->assertTimesInvoked(2); $callable->assertCalled(function (Depedency $dependency) use ($expected): bool { return $dependency->is($expected[0]); }); $callable->assertCalled(function (Dependency $dependency) use ($expected): bool { return $dependency->is($expected[1]); }); }
該包提供了諸如 assertCalled、assertNotCalled、assertInvoked 等斷言。有關(guān)詳細(xì)信息和示例,請(qǐng)務(wù)必查看項(xiàng)目自述文件中的可用 assertions 的完整列表。
以上就是callable-fake的詳細(xì)介紹,代碼詳細(xì)清楚,如果在日常工作遇到這個(gè)問(wèn)題,希望你能通過(guò)這篇文章解決問(wèn)題。如果想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。