将方法签名与模拟类中的未知参数进行匹配

Nei*_*ard 1 mockito dart flutter-test

我有这个方法:

\n\n
Future<Either<Failure, WorkEntity>> updateWorkEntity({int id, String title, TimeType timeType, int times, DateTime executed})\n
Run Code Online (Sandbox Code Playgroud)\n\n

被这样称呼:

\n\n
repository.updateWorkEntity(id: workEntity.id, executed: DateTime.now())\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
when(repository.updateWorkEntity(id: expected.id, executed: any)).thenAnswer((_) async => Right(expected));\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
\n

when无效参数:“任何”参数匹配器在方法存根(通过)或验证(通过verifyor\n )之外使用untilCalled。这是无效的,并且会在下一次存根或验证期间导致不良行为。

\n
\n\n

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

\n\n

谢谢
\nS\xc3\xb8ren

\n

Ben*_*ers 5

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