Laravel 8新特性之“時間旅行”

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

下面由Laravel教程欄目給大家介紹Laravel 8 的新特性“時間旅行” ,希望對需要的朋友有所幫助!

Laravel 8新特性之“時間旅行”

laravel 8 帶來的另一個有幫助的小特性是在測試環節,你可以流暢的進行時間穿梭。

這個特性可以讓你更容易對一些情況進行測試:例如當免費試用結束時會發生什么,或者下一個賬單日期會發生什么,等等……enunomaduro 在 twitter 上分享了一個簡單的示例,演示了這個新特性的用法:

//?穿越到?5?分鐘后 $this->travel(5)->minutes; $this->get($route)->assertSee('Created?5?mins?ago');  //?穿越到?1?年后 $this->travel(1)->year; $this->get($route)->assertSee('Created?1?year?ago');  //?穿越到制定日期 $this->travelTo($user->trial_ends_at); $this->get($route)->assertSee('Your?free?trial?is?expired');

這個特性實際上是對 Carbon 的 setTestNow 方法的一個封裝,但是在設計上會更加流暢。當然了,用不用這個特性都可以,這是一些為了讓你的開發生活更美好的小事情之一。

原文地址:https://laravel-news.com/laravel-time-traveling

? 版權聲明
THE END
喜歡就支持一下吧
點贊13 分享