【发布时间】:2011-07-24 13:11:30
【问题描述】:
我正在使用 Moq 为项目编写单元测试,当我尝试验证 DateTime 属性是否已分配值时,其中一个测试失败。这是我的验证(失败):
_mockTaskContext.Verify(context => context.TaskQueue.AddObject(It.Is<TaskQueueItem>(
task_queue => task_queue.TaskCode == (int)TaskCode.MyTask
&& task_queue.ClientID == ExpectedClientID
&& task_queue.JobNumber == It.IsAny<int>()
&& task_queue.Requester == String.Empty
&& task_queue.JobStatus == (int)JobStatus.Submitted
&& task_queue.TimeQueued == It.IsAny<DateTime>()
&& task_queue.TimeStarted == new DateTime(1900, 1, 1)
&& task_queue.TimeStopped == new DateTime(1900, 1, 1)
&& task_queue.TaskParameters == expectedTaskParam
)), Times.Once());
如果我在task_queue.TimeQueued 上注释掉预期,那么测试通过,而无需对我的测试进行任何其他更改。另外,如果我将TimeStarted 或TimeStopped 的要求从new DateTime(1900, 1, 1) 更改为It.IsAny<DateTime>(),测试将失败。我已经使用实际实现而不是模拟存储库在单元测试之外运行了被测代码,并且 TimeQueued 被正确分配了它的值。知道为什么 It.IsAny 似乎无法对 DateTime 属性正常工作,还是我的期望设置不正确?
更新:我在其他测试中使用 It.IsAny() 没有任何问题,但这个测试仍然失败。我认为这可能是因为它在 It.Is lambda 表达式中,但我不知道如何解决这个问题。
【问题讨论】: