MOQ- Setting up and verifying a generic method with Func argument

邮差的信 提交于 2021-01-28 19:34:21

问题


I have a third party interface which I want to mock its methods. To make my purpose clear consider the following IFoo interface which has a generic method like M2. One of M2 arguments is of type Func.

        public interface IFoo
        {
            bool M1<T>();
            bool M2<T>(T arg, Func<T, string> func);
        }

If I set up the M2 method as:

            var mock = new Mock<IFoo>();
            mock.Setup(foo => foo.M2(It.IsAny<It.IsAnyType>(),It.IsAny<Func<It.IsAnyType, string>>())).Returns(true);
            mock.Object.M2("arg1", s => s);
            mock.Verify(foo => foo.M2(It.IsAny<It.IsAnyType>(), It.IsAny<Func<It.IsAnyType, string>>()));

then verify will fail. But if it is set up and verified with a specific type like string then it works:

            mock.Setup(foo => foo.M2(It.IsAny<string>(), It.IsAny<Func<string, string>>())).Returns(true);
            mock.Object.M2("arg1", s => s);
            mock.Verify(foo => foo.M2(It.IsAny<string>(), It.IsAny<Func<string, string>>()));

The problem is that the actual type of T passed to my Mock is an internal class defined in that third party library. So I can't set up and verify with a specific type like the above-mentioned one.

Am I missing something in my first set up or verify or it is a well-known issue which has not been addressed yet? I am using moq 4.13.1 and my test project is .Net Core 3.1


回答1:


Rather than

It.IsAny<Func<It.IsAnyType, string>>()

for the Func parameter, try

(Func<It.IsAnyType, string>) It.IsAny<object>()

Working example:

var mock = new Mock<IFoo>();
mock.Setup(foo => foo.M2(It.IsAny<It.IsAnyType>(), (Func<It.IsAnyType, string>) It.IsAny<object>())).Returns(true);

mock.Object.M2("arg1", s => s);

mock.Verify(foo => foo.M2(It.IsAny<It.IsAnyType>(), (Func<It.IsAnyType, string>) It.IsAny<object>()));

As far as I know Moq isn't able to match the Func<> type parameters using the It.IsAnyType matcher.



来源:https://stackoverflow.com/questions/61317501/moq-setting-up-and-verifying-a-generic-method-with-func-argument

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