示例#1
0
        public void RulesExceptionUseCaseModelFoo()
        {
            var modelFoo = new ModelFoo
            {
                ModelFooId = 1,
                Name       = "Test",
                ModelBar   = new ModelBar
                {
                    ModelBarId = 1,
                    Name       = "Test"
                }
            };

            var rulesException = new RulesException <ModelFoo>();

            // This will apply to the model as whole and should be used for
            // scenarios where there are multiple issues against another object.
            rulesException.ErrorForModel("There is already a ModelFoo with this Id and Name");
            Assert.Single(rulesException.Errors);

            // Should be used for property issues.
            rulesException.ErrorFor(x => x.Name, "The Name is not Unique");
            rulesException.ErrorFor(x => x.ModelBar.Name, "The Name is not Unique");

            rulesException.ErrorFor("Name", "Another Error");

            var errorMessage = rulesException.ToString();

            Assert.Equal(4, rulesException.Errors.Count());
        }