如何最小起订量索引属性

How to MOQ an Indexed property(如何最小起订量索引属性)
本文介绍了如何最小起订量索引属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试模拟对索引属性的调用.IE.我想要起订量以下:

I am attempting to mock a call to an indexed property. I.e. I would like to moq the following:

object result = myDictionaryCollection["SomeKeyValue"];

还有setter值

myDictionaryCollection["SomeKeyValue"] = myNewValue;

我这样做是因为我需要模拟我的应用使用的类的功能.

I am doing this because I need to mock the functionality of a class my app uses.

有人知道如何使用最小起订量来做到这一点吗?我尝试了以下变体:

Does anyone know how to do this with MOQ? I've tried variations on the following:

Dictionary<string, object> MyContainer = new Dictionary<string, object>();
mock.ExpectGet<object>( p => p[It.IsAny<string>()]).Returns(MyContainer[(string s)]);

但这不会编译.

我想通过 MOQ 实现的目标是否可行,有没有人可以举例说明我如何做到这一点?

Is what I am trying to achieve possible with MOQ, does anyone have any examples of how I can do this?

推荐答案

看来我试图用 MOQ 做的事情是不可能的.

It appears that what I was attempting to do with MOQ is not possible.

基本上我是在尝试最小化一个 HTTPSession 类型的对象,其中被设置为索引的项的键只能在运行时确定.访问索引属性需要返回先前设置的值.这适用于基于整数的索引,但基于字符串的索引不起作用.

Essentially I was attempting to MOQ a HTTPSession type object, where the key of the item being set to the index could only be determined at runtime. Access to the indexed property needed to return the value which was previously set. This works for integer based indexes, but string based indexes do not work.

这篇关于如何最小起订量索引属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Mocking generic methods in Moq without specifying T(在 Moq 中模拟泛型方法而不指定 T)
How Moles Isolation framework is implemented?(Moles Isolation 框架是如何实现的?)
Difference between Dependency Injection and Mocking Framework (Ninject vs RhinoMocks or Moq)(依赖注入和模拟框架之间的区别(Ninject vs RhinoMocks 或 Moq))
How to mock Controller.User using moq(如何使用 moq 模拟 Controller.User)
How do I mock a class without an interface?(如何模拟没有接口的类?)
Mocking Static methods using Rhino.Mocks(使用 Rhino.Mocks 模拟静态方法)