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 方法的一个封装,但是在设计上会更加流畅。当然了,用不用这个特性都可以,这是一些为了让你的开发生活更美好的小事情之一。
我的博客:dmmylove.cn/articles/61
原文:laravel-news.com/laravel-time-trav...
公众号:前端猫哥
本作品采用《CC 协议》,转载必须注明作者和本文链接
长路漫漫,唯键盘作伴。:octocat: 我是猫哥,欢迎关注我的 「个人博客 」和微信公众号「前端猫哥」 :new_moon_with_face:
推荐文章: