如何最小起订量索引属性

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

问题描述

限时送ChatGPT账号..

我正在尝试模拟对索引属性的调用.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.

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

上一篇:在 Moq 中模拟泛型方法而不指定 T 下一篇:在 c# 中使用 Moq 进行模拟

相关文章

最新文章