【问题标题】:Matching a method signature with a unknown parameter in a mocked class将方法签名与模拟类中的未知参数匹配
【发布时间】:2020-04-19 20:16:44
【问题描述】:

我有这个方法:

Future<Either<Failure, WorkEntity>> updateWorkEntity({int id, String title, TimeType timeType, int times, DateTime executed})

这样称呼的:

repository.updateWorkEntity(id: workEntity.id, executed: DateTime.now())

我可以在测试中控制的 id,但我当然不能控制“DateTime.now()”。我在测试中尝试的是这个:

when(repository.updateWorkEntity(id: expected.id, executed: any)).thenAnswer((_) async => Right(expected));

通过使用“any”代替“DateTime.now()”,让我的模拟返回一个对象用于我的测试,但我收到此错误:

无效参数:“任何”参数匹配器在外部使用 方法存根(通过when)或验证(通过verifyuntilCalled)。这是无效的,并导致在 下一个存根或验证。

所以我想我不能在这里使用任何东西,但是当我不控制输入参数之一时,如何让我的模拟返回一个对象?

谢谢
索伦

【问题讨论】:

    标签: dart mockito flutter-test


    【解决方案1】:

    使用executed: anyNamed('executed') 而不是executed: any

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-15
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多