I would like to write a testcase to ensure use of the toLocal function in a Flutter app I am working on as forgetting that has been a reoccuring issue. Note: I am not asking how to test the function, I am sure the Dart developers are doing a great job on that. I want to write a testcase that ensures the function is called.
Figures, it should be as easy as something like this:
// Function I want to test
int getLocalHour(DateTime dateTime) => dateTime.toLocal().hour;
void main() {
test('getLocalHour uses local time', () {
final testedTime = DateTime.utc(2022, 9, 20, 12);
expect(getLocalHour(testedTime), 14);
});
}
Now you might say "Hold up, 12 o'clock utc isn't 2pm in my timezone" and you may be right! That's the problem I have; I couldn't find a reliable way of setting the timezone to use for toLocal to get reproducible results. In CEST, this test will run fine on my laptop, during winter, it will fail. And in the CI, it will fail all year around because it's running on a server configured to use UTC time.
Is there a way to have toLocal produce a predetermined output so I can ensure it's called in the places I need it to be called?