What is the difference between passing It.IsAny() and the value of It.IsAny() to a method setup

后端 未结 2 858
长情又很酷
长情又很酷 2020-11-30 11:15

I\'m using Moq and want to create builder classes to create my mocks with preset reasonable defaults that can be overridden during test setup as needed. The approach I took

2条回答
  •  無奈伤痛
    2020-11-30 11:29

    It.IsAny() has return type of int and returns 0, so your second setup is equivalent to:

    mock.Setup(x => x.Bar2(0)).Returns(true);
    

    I didn't check the moq code, but I'm pretty sure that when it evaluates the expression in the setup method, it takes into account that the parameter is actually It.IsAny vs. a normal number.

    You are better off if you create the setups directly in your helper methods, and not pass It.IsAny.

提交回复
热议问题