PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/mvc3/test/MvcFuturesTest/Mvc/ModelBinding/Test/DictionaryModelBinderTest.cs

https://github.com/jeanlyn/ASP.NET-Mvc-3
C# | 51 lines | 41 code | 7 blank | 3 comment | 0 complexity | 9158b2e380f2c468bf9bcd73cca34961 MD5 | raw file
  1. namespace Microsoft.Web.Mvc.ModelBinding.Test {
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Web.Mvc;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Microsoft.Web.UnitTestUtil;
  7. using Moq;
  8. using ModelBinderProviderCollection = Microsoft.Web.Mvc.ModelBinding.ModelBinderProviderCollection;
  9. [TestClass]
  10. public class DictionaryModelBinderTest {
  11. [TestMethod]
  12. public void BindModel() {
  13. // Arrange
  14. ControllerContext controllerContext = new ControllerContext();
  15. ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
  16. ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(IDictionary<int, string>)),
  17. ModelName = "someName",
  18. ModelBinderProviders = new ModelBinderProviderCollection(),
  19. ValueProvider = new SimpleValueProvider() {
  20. { "someName[0]", new KeyValuePair<int, string>(42, "forty-two") },
  21. { "someName[1]", new KeyValuePair<int, string>(84, "eighty-four") }
  22. }
  23. };
  24. Mock<IExtensibleModelBinder> mockKvpBinder = new Mock<IExtensibleModelBinder>();
  25. mockKvpBinder
  26. .Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
  27. .Returns(
  28. delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
  29. mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType);
  30. return true;
  31. });
  32. bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(KeyValuePair<int, string>), mockKvpBinder.Object, false /* suppressPrefixCheck */);
  33. // Act
  34. bool retVal = new DictionaryModelBinder<int, string>().BindModel(controllerContext, bindingContext);
  35. // Assert
  36. Assert.IsTrue(retVal);
  37. IDictionary<int, string> dictionary = bindingContext.Model as IDictionary<int, string>;
  38. Assert.IsNotNull(dictionary);
  39. Assert.AreEqual(2, dictionary.Count);
  40. Assert.AreEqual("forty-two", dictionary[42]);
  41. Assert.AreEqual("eighty-four", dictionary[84]);
  42. }
  43. }
  44. }