AddModelStateErrors() public method

public AddModelStateErrors ( ModelStateDictionary modelState, string prefix ) : void
modelState ModelStateDictionary
prefix string
return void
コード例 #1
0
        public void Ensures_NonNull_Value_Is_In_ModelState_For_Each_Key()
        {
            // Arrange
            var ex = new RulesException("myProp", "myError");
            var modelState = new ModelStateDictionary();

            // Act
            ex.AddModelStateErrors(modelState, null);

            // Assert
            Assert.Equal(1, modelState.Keys.Count());
            Assert.NotNull(modelState["myProp"].Value);
        }
コード例 #2
0
        public void Does_Not_Overwrite_Any_Existing_ModelState_Value()
        {
            // Arrange
            object rawValue = new object();
            var someValue = new ModelState { Value = new ValueProviderResult(rawValue, null, null)};
            var ex = new RulesException("myProp", "myError");
            var modelState = new ModelStateDictionary();
            modelState.Add("myProp", someValue);

            // Act
            ex.AddModelStateErrors(modelState, null);

            // Assert
            Assert.Equal(1, modelState.Keys.Count());
            Assert.Same(rawValue, modelState["myProp"].Value.RawValue);
        }
コード例 #3
0
        public void Populates_ModelState_Without_Prefix()
        {
            // Arrange
            var someObj = new object();
            var ex = new RulesException(new[] {
                new ErrorInfo("p1", "e1"),
                new ErrorInfo("p2", "e2", someObj),
                new ErrorInfo("p2", "e3"),
                new ErrorInfo("p2", "e4", someObj),
                new ErrorInfo("p3", "e5", someObj)
            });
            var modelState = new ModelStateDictionary();

            // Act
            ex.AddModelStateErrors(modelState, null, x => x.Object == someObj);

            // Assert
            Assert.Equal(2, modelState.Keys.Count());
            Assert.Equal(2, modelState["p2"].Errors.Count);
            Assert.Equal(1, modelState["p3"].Errors.Count);
            Assert.Equal("e2", modelState["p2"].Errors[0].ErrorMessage);
            Assert.Equal("e4", modelState["p2"].Errors[1].ErrorMessage);
            Assert.Equal("e5", modelState["p3"].Errors[0].ErrorMessage);
        }