コード例 #1
0
        public void IsValidPropertyReturnsFalseIfErrors()
        {
            // Arrange
            ModelState errorState = new ModelState()
            {
                Value = HtmlHelperTest.GetValueProviderResult("quux", "quux")
            };

            errorState.Errors.Add("some error");
            ModelStateDictionary dictionary = new ModelStateDictionary()
            {
                {
                    "foo",
                    new ModelState()
                    {
                        Value = HtmlHelperTest.GetValueProviderResult("bar", "bar")
                    }
                },
                { "baz", errorState }
            };

            // Act
            bool isValid = dictionary.IsValid;

            // Assert
            Assert.False(isValid);
        }
コード例 #2
0
        public void IsValidPropertyReturnsTrueIfNoErrors()
        {
            // Arrange
            ModelStateDictionary dictionary = new ModelStateDictionary()
            {
                {
                    "foo",
                    new ModelState()
                    {
                        Value = HtmlHelperTest.GetValueProviderResult("bar", "bar")
                    }
                },
                {
                    "baz",
                    new ModelState()
                    {
                        Value = HtmlHelperTest.GetValueProviderResult("quux", "bar")
                    }
                }
            };

            // Act
            bool isValid = dictionary.IsValid;

            // Assert
            Assert.True(isValid);
        }
コード例 #3
0
        private static RequestContext GetRequestContext()
        {
            HttpContextBase httpcontext = HtmlHelperTest.GetHttpContext("/app/", null, null);
            RouteData       rd          = new RouteData();

            return(new RequestContext(httpcontext, rd));
        }
コード例 #4
0
        public void SetAttemptedValueCreatesModelStateIfNotPresent()
        {
            // Arrange
            ModelStateDictionary dictionary = new ModelStateDictionary();

            // Act
            dictionary.SetModelValue("some key", HtmlHelperTest.GetValueProviderResult("some value", "some value"));

            // Assert
            Assert.Single(dictionary);
            ModelState modelState = dictionary["some key"];

            Assert.Empty(modelState.Errors);
            Assert.Equal("some value", modelState.Value.ConvertTo(typeof(string)));
        }
コード例 #5
0
        public void ConstructorWithDictionaryParameter()
        {
            // Arrange
            ModelStateDictionary oldDictionary = new ModelStateDictionary()
            {
                { "foo", new ModelState()
                  {
                      Value = HtmlHelperTest.GetValueProviderResult("bar", "bar")
                  } }
            };

            // Act
            ModelStateDictionary newDictionary = new ModelStateDictionary(oldDictionary);

            // Assert
            Assert.Single(newDictionary);
            Assert.Equal("bar", newDictionary["foo"].Value.ConvertTo(typeof(string)));
        }
コード例 #6
0
        public void SetAttemptedValueUsesExistingModelStateIfPresent()
        {
            // Arrange
            ModelStateDictionary dictionary = new ModelStateDictionary();

            dictionary.AddModelError("some key", "some error");
            Exception ex = new Exception();

            // Act
            dictionary.SetModelValue("some key", HtmlHelperTest.GetValueProviderResult("some value", "some value"));

            // Assert
            Assert.Single(dictionary);
            ModelState modelState = dictionary["some key"];

            Assert.Single(modelState.Errors);
            Assert.Equal("some error", modelState.Errors[0].ErrorMessage);
            Assert.Equal("some value", modelState.Value.ConvertTo(typeof(string)));
        }
コード例 #7
0
        private static UrlHelper GetUrlHelper()
        {
            HttpContextBase httpcontext = HtmlHelperTest.GetHttpContext("/app/", null, null);
            RouteCollection rt          = new RouteCollection();

            rt.Add(new Route("{controller}/{action}/{id}", null)
            {
                Defaults = new RouteValueDictionary(new { id = "defaultid" })
            });
            rt.Add("namedroute", new Route("named/{controller}/{action}/{id}", null)
            {
                Defaults = new RouteValueDictionary(new { id = "defaultid" })
            });
            RouteData rd = new RouteData();

            rd.Values.Add("controller", "home");
            rd.Values.Add("action", "oldaction");

            UrlHelper urlHelper = new UrlHelper(new RequestContext(httpcontext, rd), rt);

            return(urlHelper);
        }