如何在Cats Effect 3的测试中让虚拟时间通过?
How to make virtual time pass in a test in Cats Effect 3?
我正在尝试转换 Mules to Cats Effect 3 (CE3). Since it is a caching library, in its tests it needs (virtual) time to pass to test whether items will be expired. It is currently making fairly heavy use of cats.effect.laws.util.TestContext
, which allows advancing the virtual clock without actually sleeping via tick
. TestContext
does not (as far as I have been able to discover) exist in CE3, but I am always loathe to puts sleep
calls into a test. Rolling my own IO
for this specific case though is a daunting prospect; surely this is a more general problem people have. I was not able to find a reference to TestContext
in the migration guide,但是。
这种情况是否有已知的升级路径?
在 Discord 上 Chris Davenport 的帮助下,我发现 TestContext
仍然存在,但已从 cats.effect.laws.util
移至 cats.effect.kernel.testkit
。它存在于自己的项目和工件中,因此您需要添加依赖项(SBT 语法):
"org.typelevel" %% "cats-effect-kernel-testkit" % catsEffectV % Test,
此外,如果您没有使用 laws
包中的任何其他内容,那么您将想要删除该依赖项,它看起来像:
"org.typelevel" %% "cats-effect-laws" % catsEffectV % Test,
大概这就是他们移动它的原因:因为它不是特定于法律的。
但是,我没有成功地获得虚拟时间来通过我的测试,遗憾的是现在用 IO.sleep
s 强行使用它。 :/
正如 Victor 在关于 v3.3.0 的评论中提到的,这个版本大约在一天前发布,您可以在文档 Mocking time 的段落中看到虚拟时间的示例
我正在尝试转换 Mules to Cats Effect 3 (CE3). Since it is a caching library, in its tests it needs (virtual) time to pass to test whether items will be expired. It is currently making fairly heavy use of cats.effect.laws.util.TestContext
, which allows advancing the virtual clock without actually sleeping via tick
. TestContext
does not (as far as I have been able to discover) exist in CE3, but I am always loathe to puts sleep
calls into a test. Rolling my own IO
for this specific case though is a daunting prospect; surely this is a more general problem people have. I was not able to find a reference to TestContext
in the migration guide,但是。
这种情况是否有已知的升级路径?
在 Discord 上 Chris Davenport 的帮助下,我发现 TestContext
仍然存在,但已从 cats.effect.laws.util
移至 cats.effect.kernel.testkit
。它存在于自己的项目和工件中,因此您需要添加依赖项(SBT 语法):
"org.typelevel" %% "cats-effect-kernel-testkit" % catsEffectV % Test,
此外,如果您没有使用 laws
包中的任何其他内容,那么您将想要删除该依赖项,它看起来像:
"org.typelevel" %% "cats-effect-laws" % catsEffectV % Test,
大概这就是他们移动它的原因:因为它不是特定于法律的。
但是,我没有成功地获得虚拟时间来通过我的测试,遗憾的是现在用 IO.sleep
s 强行使用它。 :/
正如 Victor 在关于 v3.3.0 的评论中提到的,这个版本大约在一天前发布,您可以在文档 Mocking time 的段落中看到虚拟时间的示例