2

试图用 mockk 模拟 OffsetDateTime.now(),但它抛出

Missing calls inside every { ... } block

我试过了:

staticMockk<OffsetDateTime>().mock().run {
  val mockTime = OffsetDateTime.now()
  every { OffsetDateTime.now() } returns mockTime
  // test
}

也试过:

mockkStatic(OffsetDateTime::class)
val mockTime = OffsetDateTime.now()
every { OffsetDateTime.now() } returns mockTime
4

1 回答 1

2

对我来说没关系。请考虑使用最新版本(1.8.8 或 1.8.8.kotlin13),或者如果它不起作用,请报告错误。

这是我的代码:

mockkStatic(OffsetDateTime::class)

val date = OffsetDateTime.of(
    2012,
    10,
    10,
    10,
    10,
    10,
    10,
    ZoneOffset.UTC
)

every {
    OffsetDateTime.now()
} returns date

println(OffsetDateTime.now())
于 2018-09-29T07:20:26.050 回答