testing - Is there a way to fake DateTime.now() in a Flutter test?
问题描述
I can pass in a time to my widget, but my widget is supposed to be instantiating the time in itself, not receiving a DateTime from a parent widget.
解决方案
extension CustomizableDateTime on DateTime {
static DateTime customTime;
static DateTime get current {
if (customTime != null)
return customTime;
else
return DateTime.now();
}
}
只需CustomizableDateTime.current
在生产代码中使用。您可以像这样在测试中修改返回值:CustomizableDateTime.customTime = DateTime.parse("1969-07-20 20:18:04");
. 无需使用第三方库。
推荐阅读
- visual-studio - 如果可用,将 VersionSuffix 设置为提交 id
- c++ - 如何使用 gcov 生成覆盖信息的 HTML 报告?
- python - 如何在 lambda 函数中将变量从 NoneType 更改为 Float?
- angular - 错误:NullInjectorError:没有 CompilerFactory 的提供者!当我尝试在同一页面中运行 2 个角度元素时
- node.js - 读取 csv 时返回非 utf-8 响应
- amazon-web-services - AWS Elasticsearch vpc 端点和私有 vpc 域
- python - 如何重塑未对齐的数据集并用 numpy 丢弃剩余数据?
- javascript - 正则表达式匹配 Javascript 中的版本号
- python - 如何使用 Jinja2 单独访问字典列表中的每个字典?
- substrate - Parity Substrate - 区块尚未最终确定