How to Unit Test a custom ModelBinder using Moq?

血红的双手。 提交于 2019-12-02 22:10:26

The culprit is this line:

httpContext.Setup(c => c.Request.RequestType).Returns(
                verbs.ToString().ToUpper()
            );

This is technically a second Setup on the Request object, and it is wiping out the original Setup, even though you're going "past" it in the object hierarchy. I'm not sure if this is a bug in Moq or desired behaviour, I've run into this before as well and haven't gotten around to checking it out.

You can solve it by moving that line to where you're setting up your request above, and setting it up directly, rather than by going through the httpContext. So,

request.Setup(c => c.RequestType).Returns(verbs.ToString().ToUpper());

I also notice that the "var u" that you declare is not being used ;)

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