如何起订量索引属性 [英] How to MOQ an Indexed property

查看:117
本文介绍了如何起订量索引属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图嘲弄到索引属性的调用。即我想起订量如下:

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

object result = myDictionaryCollection["SomeKeyValue"];

和也二传手值

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



但是,这并不编译。

But that doesn't compile.

就是我想用最小起订量来实现可能的,没有任何人有我怎么能做到这一点的例子?

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

推荐答案

据看来我试图用最小起订量做是不可能的。

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

从本质上讲,我试图MOQ一个的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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆