How to Unit Test a custom ModelBinder using Moq?

前端 未结 1 928
醉酒成梦
醉酒成梦 2021-02-02 17:23

I\'m having some difficulty writing some Unit Tests to test a custom ModelBinder that I created. The ModelBinder I\'m trying to Unit Test is the JsonDictionaryModelBinder that I

相关标签:
1条回答
  • 2021-02-02 17:47

    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 ;)

    0 讨论(0)
提交回复
热议问题