将多个索引器与 Moq 一起使用

发布于 2024-12-11 18:25:17 字数 1885 浏览 0 评论 0原文

我一直在尝试使用 Moq 来伪造具有多个索引器的对象集(并获取)。我之前已经将 Moq 与单个索引器一起使用相当长一段时间了,但它似乎不适用于多个索引器。我从我的研究中了解到,Moq 使用 It.IsAny<> 可能会出现问题。对于索引器参数,但我还尝试了具有特定参数的以下代码(例如mock[1,“BlockItem”])这是我的代码:

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; });
                       });

无论我在SetupSet中为索引器函数提供什么参数,这都会再次生成以下异常()。

初始化方法UnitTest.BonusHandlerTest.MyTestInitialize抛出 例外。 System.ArgumentNullException: System.ArgumentNullException:值不能为空。参数名称: 论据。

System.Linq.Expressions.Expression.ValidateArgumentTypes(MethodInfo 方法,ReadOnlyCollection1&论据) System.Linq.Expressions.Expression.ValidateCallArgs(表达式 实例,MethodInfo方法,ReadOnlyCollection1&论据) System.Linq.Expressions.Expression.Call(表达式实例, MethodInfo 方法,IEnumerable1 参数) System.Linq.Expressions.Expression.Call(表达式实例, MethodInfo 方法,Expression[] 参数)TCall](Mock1 模拟, Action1 setterExpression, Func5 callFactory) b__25() Moq.PexProtector.Invoke[T](Func1 函数) Moq.Mock.SetupSet[T](Mock1 模拟,Action1 setterExpression,Func1 条件)SetupSet(Action`1 setterExpression) UnitTest.BonusHandlerTest.SetupPersistence() 中 C:\perforce\dev\KHIRST_Client12.BonusHandler\Client12\Gaming\BonusHandler\UnitTest\BonusHandlerTest.cs: 第 868 行 UnitTest.BonusHandlerTest.MyTestInitialize() 中 C:\perforce\dev\KHIRST_Client12.BonusHandler\Client12\Gaming\BonusHandler\UnitTest\BonusHandlerTest.cs: 第 100 行

根据我的尝试,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, ReadOnlyCollection
1& arguments)
System.Linq.Expressions.Expression.Call(Expression instance,
MethodInfo method, IEnumerable1 arguments)
System.Linq.Expressions.Expression.Call(Expression instance,
MethodInfo method, Expression[] arguments) TCall](Mock
1 mock,
Action1 setterExpression, Func5 callFactory) b__25()
Moq.PexProtector.Invoke[T](Func1 function)
Moq.Mock.SetupSet[T](Mock
1 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一向肩并 2024-12-18 18:25:17

不管怎样,我设置了这个场景,没有例外:

        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.IsAny<> 那样。对于索引器的参数,还可以设置值。我从未在测试中尝试这样做,但这对我来说似乎是该工具当前的限制。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文