将多个索引器与 Moq 一起使用
我一直在尝试使用 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 方法,ReadOnlyCollection
1&论据) System.Linq.Expressions.Expression.ValidateCallArgs(表达式 实例,MethodInfo方法,ReadOnlyCollection
1&论据) System.Linq.Expressions.Expression.Call(表达式实例, MethodInfo 方法,IEnumerable1 参数) System.Linq.Expressions.Expression.Call(表达式实例, MethodInfo 方法,Expression[] 参数)TCall](Mock
1 模拟, Action1 setterExpression, Func
5 callFactory) b__25() Moq.PexProtector.Invoke[T](Func1 函数) Moq.Mock.SetupSet[T](Mock
1 模拟,Action1 setterExpression,Func
1 条件)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)
1& arguments)
System.Linq.Expressions.Expression.ValidateCallArgs(Expression
instance, MethodInfo method, ReadOnlyCollection
System.Linq.Expressions.Expression.Call(Expression instance,
MethodInfo method, IEnumerable1 arguments)
1 mock,
System.Linq.Expressions.Expression.Call(Expression instance,
MethodInfo method, Expression[] arguments) TCall](Mock
Action1 setterExpression, Func
5 callFactory) b__25()
Moq.PexProtector.Invoke[T](Func1 function)
1 mock, Action
Moq.Mock.SetupSet[T](Mock1 setterExpression, Func
1
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不管怎样,我设置了这个场景,没有例外:
看起来它不只是像 It.IsAny<> 那样。对于索引器的参数,还可以设置值。我从未在测试中尝试这样做,但这对我来说似乎是该工具当前的限制。
For what it's worth, I set this scenario up and there were no exceptions:
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.