有时候我们需要对一些超时的逻辑进行测试,需要等待一定的时间来验证超时逻辑是否生效。

 

Carbon 库提供了 setTestNow 方法来设置一个虚拟的当前时间

使用这个特性的前提是:我们的待测试代码利用 Carbon::now() 来获取当前时间(这种情况获取到的当前时间是我们设置的一个虚拟的当前时间)。

public function test_items_can_expire()
    {
        Carbon::setTestNow(Carbon::now());
        $store = new ArrayStore;

        $store->put('foo', 'bar', 10);
        Carbon::setTestNow(Carbon::now()->addSeconds(10)->addSecond());
        $result = $store->get('foo');

        $this->assertNull($result);
        Carbon::setTestNow(null);
    }

 

相关文章:

  • 2021-12-17
  • 2021-10-22
  • 2021-06-13
  • 2021-11-18
  • 2022-12-23
  • 2021-04-14
  • 2021-11-01
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案