Moq unit test with It.IsAny<DateTime>() fails

北战南征 提交于 2019-12-06 10:29:37

I am sure the It.IsAny<>() syntax must be used within the scope of the mock object. In this case when you use Setup and the mock arguments directly. This is because the mock object is in recording mode capturing the values you pass into arguments so

mock.Setup(x => x.Foo(It.IsAny<Bar>()));

will process the arguments when the Setup line is executed.

However in your example you are trying to use It.IsAny<>() from within the delegate to verify an argument passed in matches. When this occurs the mock is not recording but in the process of being used as a result of the object under test (which is much later).

So someValue == It.IsAny<DateTime>() cannot evaluate to true as the return of the IsAny method must return a matching value for it to be true. I expect that It.IsAny<int>() also does not work.

My suggestion is that you will have to match either exact values or in this case match a range of dates

&& IsInRange(DateTime.MinValue, DateTime.MaxValue, task_queue.TimeQueued)

where IsInRange simply another method you have for checking a value is between 2 min and max bounds.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!