简体   繁体   English

使用Moq It.IsAny使用通用Lambda设置Mock用于泛型函数

[英]Setup Mock for generic function with generic Lambda using Moq It.IsAny

I am trying to mock this interface: 我试图模仿这个界面:

public interface IManager
{
    TVal GetOrAdd<TVal, TArg>(string key, Func<TArg, TVal> valueFactory, TArg valueFactoryArg) where TVal : class;
}

And i am having isuse to mock the lambda expression. 我正在使用模拟lambda表达式。

var _menagerMock = new Mock<IManager>();
_menagerMock.Setup(x => x.GetOrAdd<string, Tuple<int>>("stringValue",
            It.IsAny<Func<Tuple<int>,string>>, It.IsAny<Tuple<int>>);

the It.IsAny< Func,string>> is not passing compilation, and the error is: Expected a method with 'string IsAny(Tuple)' signature . It.IsAny <Func,string >>没有通过编译,错误是: 预期带有'string IsAny(Tuple)'签名的方法

Is it possible to mock this kind of function? 有可能嘲笑这种功能吗?

Try: 尝试:

        var _menagerMock = new Mock<IManager>();
        _menagerMock.Setup(x => x.GetOrAdd("stringValue",
            It.IsAny<Func<Tuple<int>, string>>(), It.IsAny<Tuple<int>>()));

Edit: As an aside, It.IsAny() is not a best practice for testing. 编辑:另外,It.IsAny()不是测试的最佳实践。 You should be setting up explicit values instead of relying on It.IsAny(). 您应该设置显式值而不是依赖It.IsAny()。 If you're not really sure of the inputs in your tests, how can you be sure that you're getting valid output? 如果您对测试中的输入不是很确定,那么如何确保获得有效输出?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM