Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using multiple indexers with Moq

I have been trying to use Moq to fake an object set (and get) with multiple indexers. I have previously been using Moq with single indexers for quite some time, but it doesn't seem to be working using multiple indexers. I am aware from my research that Moq can have a problem using It.IsAny<> for indexer parameters, but I have also tried the following code with specific parameters (like mock[1, "BlockItem"]) Here is my code:

m_storageAccessor.SetupSet(
mock => mock[It.IsAny<int>(), It.IsAny<string>()] = It.IsAny<object>()).Callback(
                (int i, string s, object o) =>
                       {
                           m_storageAccessor.SetupGet(
                               mock => mock[i, s]).Returns(
                                   () => { return o; });
                       });

This generates the following exception, again, no matter what parameters I give the indexer function in SetupSet().

Initialization method UnitTest.BonusHandlerTest.MyTestInitialize threw exception. System.ArgumentNullException: System.ArgumentNullException: Value cannot be null. Parameter name: arguments.

System.Linq.Expressions.Expression.ValidateArgumentTypes(MethodInfo method, ReadOnlyCollection1& arguments) System.Linq.Expressions.Expression.ValidateCallArgs(Expression instance, MethodInfo method, ReadOnlyCollection1& arguments) System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable1 arguments) System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, Expression[] arguments) TCall](Mock1 mock, Action1 setterExpression, Func5 callFactory) b__25() Moq.PexProtector.Invoke[T](Func1 function) Moq.Mock.SetupSet[T](Mock1 mock, Action1 setterExpression, Func1 condition) SetupSet(Action`1 setterExpression) UnitTest.BonusHandlerTest.SetupPersistence() in C:\perforce\dev\KHIRST_Client12.BonusHandler\Client12\Gaming\BonusHandler\UnitTest\BonusHandlerTest.cs: line 868 UnitTest.BonusHandlerTest.MyTestInitialize() in C:\perforce\dev\KHIRST_Client12.BonusHandler\Client12\Gaming\BonusHandler\UnitTest\BonusHandlerTest.cs: line 100

It almost seems like, based on what I have tried, that Moq is unable to do indexers with multiple parameters. Anyone have any ideas? The interwebs haven't been much help.

like image 555
Kevin Hirst Avatar asked May 29 '26 14:05

Kevin Hirst


1 Answers

For what it's worth, I set this scenario up and there were no exceptions:

        var myStub = new Mock<Foo>();
        myStub.SetupSet(foo => foo[12, "asdf"] = null).Callback((int i, string s, object o) => myStub.SetupGet(foo => foo[i, s]).Returns(o));

It appears that it doesn't just like the It.IsAny<> for the parameters to the indexer, but also to the set value. I've never tried to do this in my testing, but this seems like a current limitation of the tool to me.

like image 187
Erik Dietrich Avatar answered Jun 01 '26 03:06

Erik Dietrich