TDD - Business Rule Testing / Validation in ASP.NET MVC

I use sharp architecture, so I can easily use mocks, etc. in their unit tests and / or during TDD. I have quite complex business rules and want to test them at the controller level. I just wonder how other people do it?

For me, validating business rules at three levels:

(1) Level of ownership (e.g. property required) (2) Level of ownership within the facility (e.g. start date and end date) (3) Persistence level (e.g. unique name, parent child cannot be a child)

My validation system also assigns properties to properties. I'm just wondering what other people are doing. Do you write a test for each business rule and verify that the correct error message is assigned correctly (i.e. Look at the ASP.MVC ModelState)?

Hope my question makes sense. Many thanks!

Regards,

Christian

+3
source share
1 answer

I usually divide this into two:

  • Check if the controller works correctly when the model is valid and when not.
  • Test if the model, in combination with the rule engine, generates the correct error messages.

, , , , . .

, , , , , :

  [TestClass]
    public class Maintaining_todo_list
    {
        private TodoController controller;

        [TestInitialize]
        public void Setup()
        {
            controller = new TodoController {ControllerContext = new ControllerContext()};
        }

        [TestMethod]
        public void Valid_update_should_redirect_to_list()
        {
            var result = controller.Edit(1, new TodoItem {Text = "todo"});
            result.ShouldRedirectTo("list");
        }

        [TestMethod]
        public void Invalid_update_should_display_same_view()
        {
            var result = controller.Edit(1, new TodoItem {Text = ""});
            result.ShouldDisplayDefaultView();
        }
    }

:

   [TestClass]
    public class Validating_todo_item
    {
        [TestMethod]
        public void Text_cannot_be_empty()
        {
            var todo = new TodoItem {Text = ""};
            todo.ShouldContainValidationMessage("Text cannot be empty");
        }

        [TestMethod]
        public void Text_cannot_contain_more_than_50_characters()
        {
            var todo = new TodoItem { Text = new string('a', 51) };
            todo.ShouldContainValidationMessage("Text cannot contain more than 50 characters");
        }

        [TestMethod]
        public void Valid_items()
        {
            new TodoItem { Text = new string('a', 1) }.ShouldBeValid();
            new TodoItem { Text = new string('a', 50) }.ShouldBeValid();
        }
    }

( , )

 internal static class AssertionHelpers
    {
        public static void ShouldRedirectTo(this ActionResult result, string action)
        {
            var redirect = result as RedirectToRouteResult;

            Assert.IsNotNull(redirect);
            Assert.AreEqual(action, redirect.RouteValues["action"]);
            Assert.IsNull(redirect.RouteValues["controller"]);
        }

        public static void ShouldDisplayDefaultView(this ActionResult result)
        {
            var view = result as ViewResult;

            Assert.IsNotNull(view);
            Assert.AreEqual("", view.ViewName);
        }

        public static void ShouldContainValidationMessage(this TodoItem todo, string message)
        {
            var context = new ValidationContext(todo, null, null);
            var results = new List<ValidationResult>();

            Validator.TryValidateObject(todo, context, results, true);

            var errors = results.Select(result => result.ErrorMessage);

            foreach (var error in errors)
            {
                Console.Out.WriteLine(error);
            }

            Assert.IsTrue(errors.Contains(message));
        }

        public static void ShouldBeValid(this TodoItem todo)
        {
            var context = new ValidationContext(todo, null, null);
            var results = new List<ValidationResult>();
            var isValid = Validator.TryValidateObject(todo, context, results, true);
            Assert.IsTrue(isValid);
        }
    }
0

Source: https://habr.com/ru/post/1738938/


All Articles