PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/jeanlyn/ASP.NET-Mvc-3
C# | 111 lines | 86 code | 16 blank | 9 comment | 0 complexity | a4330847f50dd8a69acb75f777a380fa MD5 | raw file
  1. namespace Microsoft.Web.Mvc.ModelBinding.Test {
  2. using System.Collections.Generic;
  3. using System.Linq;
  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 KeyValuePairModelBinderTest {
  11. [TestMethod]
  12. public void BindModel_MissingKey_ReturnsFalse() {
  13. // Arrange
  14. ControllerContext controllerContext = new ControllerContext();
  15. ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
  16. ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(KeyValuePair<int, string>)),
  17. ModelName = "someName",
  18. ModelBinderProviders = new ModelBinderProviderCollection(),
  19. ValueProvider = new SimpleValueProvider()
  20. };
  21. KeyValuePairModelBinder<int, string> binder = new KeyValuePairModelBinder<int, string>();
  22. // Act
  23. bool retVal = binder.BindModel(controllerContext, bindingContext);
  24. // Assert
  25. Assert.IsFalse(retVal);
  26. Assert.IsNull(bindingContext.Model);
  27. Assert.AreEqual(0, bindingContext.ValidationNode.ChildNodes.Count, "No child nodes should have been added.");
  28. }
  29. [TestMethod]
  30. public void BindModel_MissingValue_ReturnsTrue() {
  31. // Arrange
  32. ControllerContext controllerContext = new ControllerContext();
  33. ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
  34. ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(KeyValuePair<int, string>)),
  35. ModelName = "someName",
  36. ModelBinderProviders = new ModelBinderProviderCollection(),
  37. ValueProvider = new SimpleValueProvider()
  38. };
  39. Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
  40. mockIntBinder
  41. .Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
  42. .Returns(
  43. delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
  44. mbc.Model = 42;
  45. return true;
  46. });
  47. bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, true /* suppressPrefixCheck */);
  48. KeyValuePairModelBinder<int, string> binder = new KeyValuePairModelBinder<int, string>();
  49. // Act
  50. bool retVal = binder.BindModel(controllerContext, bindingContext);
  51. // Assert
  52. Assert.IsTrue(retVal, "Want to return 'True' since the key was bound correctly and we want to push validation up.");
  53. Assert.IsNull(bindingContext.Model);
  54. CollectionAssert.AreEquivalent(new string[] { "someName.key" },
  55. bindingContext.ValidationNode.ChildNodes.Select(n => n.ModelStateKey).ToArray());
  56. }
  57. [TestMethod]
  58. public void BindModel_SubBindingSucceeds() {
  59. // Arrange
  60. ControllerContext controllerContext = new ControllerContext();
  61. ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
  62. ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(KeyValuePair<int, string>)),
  63. ModelName = "someName",
  64. ModelBinderProviders = new ModelBinderProviderCollection(),
  65. ValueProvider = new SimpleValueProvider()
  66. };
  67. Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
  68. mockIntBinder
  69. .Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
  70. .Returns(
  71. delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
  72. mbc.Model = 42;
  73. return true;
  74. });
  75. bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, true /* suppressPrefixCheck */);
  76. Mock<IExtensibleModelBinder> mockStringBinder = new Mock<IExtensibleModelBinder>();
  77. mockStringBinder
  78. .Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
  79. .Returns(
  80. delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
  81. mbc.Model = "forty-two";
  82. return true;
  83. });
  84. bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(string), mockStringBinder.Object, true /* suppressPrefixCheck */);
  85. KeyValuePairModelBinder<int, string> binder = new KeyValuePairModelBinder<int, string>();
  86. // Act
  87. bool retVal = binder.BindModel(controllerContext, bindingContext);
  88. // Assert
  89. Assert.IsTrue(retVal);
  90. Assert.AreEqual(new KeyValuePair<int, string>(42, "forty-two"), bindingContext.Model);
  91. CollectionAssert.AreEquivalent(new string[] { "someName.key", "someName.value" },
  92. bindingContext.ValidationNode.ChildNodes.Select(n => n.ModelStateKey).ToArray());
  93. }
  94. }
  95. }