Setting up a Mock for a shared function with a shared Lambda using Moq It.IsAny

I'm trying to make fun of this interface:

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

      

And I have isuse to mock a 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>>);

      

It.IsAny <Func, string -> does not commit compilation, and error: Expected method with signature 'string IsAny (Tuple)' .

Can you mock such a function?

+3


source to share


1 answer


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 best practice for testing. You should be configuring explicit values ​​instead of relying on It.IsAny (). If you're not sure what the inputs are in your tests, how can you be sure you're getting a valid result?

+4


source







All Articles