code_junkie

How to MOQ an Indexed property

Nov 12th, 2011
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. object result = myDictionaryCollection["SomeKeyValue"];
  2.  
  3. myDictionaryCollection["SomeKeyValue"] = myNewValue;
  4.  
  5. Dictionary<string, object> MyContainer = new Dictionary<string, object>();
  6. mock.ExpectGet<object>( p => p[It.IsAny<string>()]).Returns(MyContainer[(string s)]);
  7.  
  8. /// <summary>
  9. /// HTTP session mockup.
  10. /// </summary>
  11. internal sealed class HttpSessionMock : HttpSessionStateBase
  12. {
  13. private readonly Dictionary<string, object> objects = new Dictionary<string, object>();
  14.  
  15. public override object this[string name]
  16. {
  17. get { return (objects.ContainsKey(name)) ? objects[name] : null; }
  18. set { objects[name] = value; }
  19. }
  20. }
  21.  
  22. /// <summary>
  23. /// Base class for all controller tests.
  24. /// </summary>
  25. public class ControllerTestSuiteBase : TestSuiteBase
  26. {
  27. private readonly HttpSessionMock sessionMock = new HttpSessionMock();
  28.  
  29. protected readonly Mock<HttpContextBase> Context = new Mock<HttpContextBase>();
  30. protected readonly Mock<HttpSessionStateBase> Session = new Mock<HttpSessionStateBase>();
  31.  
  32. public ControllerTestSuiteBase()
  33. : base()
  34. {
  35. Context.Expect(ctx => ctx.Session).Returns(sessionMock);
  36. }
  37. }
  38.  
  39. var mock = new Mock<IDictionary>();
  40. mock.SetupGet( p => p[It.IsAny<string>()]).Returns("foo");
Add Comment
Please, Sign In to add comment