How MOQ indexed property

I am trying to make fun of a call to an indexed property. That is, I would like to do the following:

object result = myDictionaryCollection["SomeKeyValue"]; 

as well as the setting value

 myDictionaryCollection["SomeKeyValue"] = myNewValue; 

I do this because I need to make fun of the functionality of the class used by my application.

Does anyone know how to do this using MOQ? I tried the options on the following:

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

But this does not compile.

I am trying to achieve something using the MOQ, does anyone have examples of how I can do this?

+52
c # tdd moq mocking
Dec 04 '08 at 14:51
source share
5 answers

It seems that what I'm trying to do with MOQ is not possible.

Essentially, I tried MOQ to create an object of type HTTPSession, where the key of the element set to the index can only be determined at runtime. Access to the indexed property should have returned the value that was previously set. This works for integer based indexes, but row based indexes do not work.

-four
Mar 16 '09 at 8:39
source share

It is unclear what you are trying to do because you are not displaying a layout ad. Are you trying to mock a dictionary?

MyContainer[(string s)] invalid C #.

This compiles:

 var mock = new Mock<IDictionary>(); mock.SetupGet( p => p[It.IsAny<string>()]).Returns("foo"); 
+49
Dec 11 '08 at
source share

Ash, if you want an HTTP session layout, then this piece of code does the job:

 /// <summary> /// HTTP session mockup. /// </summary> internal sealed class HttpSessionMock : HttpSessionStateBase { private readonly Dictionary<string, object> objects = new Dictionary<string, object>(); public override object this[string name] { get { return (objects.ContainsKey(name)) ? objects[name] : null; } set { objects[name] = value; } } } /// <summary> /// Base class for all controller tests. /// </summary> public class ControllerTestSuiteBase : TestSuiteBase { private readonly HttpSessionMock sessionMock = new HttpSessionMock(); protected readonly Mock<HttpContextBase> Context = new Mock<HttpContextBase>(); protected readonly Mock<HttpSessionStateBase> Session = new Mock<HttpSessionStateBase>(); public ControllerTestSuiteBase() : base() { Context.Expect(ctx => ctx.Session).Returns(sessionMock); } } 
+17
May 14 '09 at 2:29
source share

It is not so difficult, but it took a little to find it :)

 var request = new Moq.Mock<HttpRequestBase>(); request.SetupGet(r => r["foo"]).Returns("bar"); 
+5
May 6 '15 at 20:09
source share

As you rightly noted, there are various SetupGet and SetupSet for initializing getters and setters, respectively. Although SetupGet intended to be used for properties, not for indexers, it will not allow you to process the key passed to it. To be precise, for indexers, SetupGet will call Setup anyway:

 internal static MethodCallReturn<T, TProperty> SetupGet<T, TProperty>(Mock<T> mock, Expression<Func<T, TProperty>> expression, Condition condition) where T : class { return PexProtector.Invoke<MethodCallReturn<T, TProperty>>((Func<MethodCallReturn<T, TProperty>>) (() => { if (ExpressionExtensions.IsPropertyIndexer((LambdaExpression) expression)) return Mock.Setup<T, TProperty>(mock, expression, condition); ... } ... } 

To answer your question, here is a sample code using a basic Dictionary to store values:

 var dictionary = new Dictionary<string, object>(); var applicationSettingsBaseMock = new Mock<SettingsBase>(); applicationSettingsBaseMock .Setup(sb => sb[It.IsAny<string>()]) .Returns((string key) => dictionary[key]); applicationSettingsBaseMock .SetupSet(sb => sb["Expected Key"] = It.IsAny<object>()) .Callback((string key, object vaue) => dictionary[key] = vaue); 

As you can see, you need to explicitly specify the key to configure the indexer installer. Details are described in another SO question: Moq indexed property and use index value in callback / callback

+3
Jun 03 '16 at 8:44
source share



All Articles