Laravel

Weap‘s logic relies on the current time in order to know when a snapshot should be created or deleted. In such cases, Carbon::now() or the Laravel helper now() is used to get the current time.

But what about when you have unit/integration tests for a piece of logic/feature that relies on Carbon::now()? For us was a problem because of some builds that started to fail based on the time they were executed.

Mocking the date-time

Luckily, Carbon has a method setTestNow() which allows you to set the time to whatever value you may need.

mock-carbon-date

Checking if the date-time was mocked

Carbon has another method hasTestNow() which may be used to check if the date-time was mocked.

check-if-carbon-was-mocked

Resetting the mocked date-time

By calling the method setTestNow() without any parameters, Carbon will reset the mocked date-time.

reset-carbon-mock

Read more