简体   繁体   中英

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.

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: .

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. You should be setting up explicit values instead of relying on 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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