ASP.NET MVC Controller post method unit test: ModelState.IsValid always true

前端 未结 2 424
南笙
南笙 2020-12-12 17:36

I have written my first unit tests for an ASP.NET MVC web application. All works fine and it is giving me valuable information, but I can\'t test errors in the view model. T

相关标签:
2条回答
  • 2020-12-12 17:50

    You're trying to test two different things at the same time. The controller is not reponsible for validating the model state, only for behaving differently based on the result of that validation. So your unit tests for the controller shouldn't try to test the validation, that should be done in a different test. In my opinion you should have three unit tests:

    1. One that verifies whether model validation correctly
    2. One that validates whether the controller behaves correctly when modelstate is valid
    3. One that validates whether the controller behaves correctly when modelstate is invalid

    Here's how you can do that:

    1.Model validation

    [Test]
    public void test_validation()
    {
        var sut = new POSViewModel();
        // Set some properties here
        var context = new ValidationContext(sut, null, null);
        var results = new List<ValidationResult>();
        var isModelStateValid =Validator.TryValidateObject(sut, context, results, true);
    
        // Assert here
    }
    

    2.Controller with invalid modelstate

    [Test]
    public void test_controller_with_model_error()
    {
        var controller = new PosController();
        controller.ModelState.AddModelError("test", "test");
    
        ActionResult result = posController.Index(new PosViewModel());
    
        // Assert that the controller executed the right actions when the model is invalid
    }
    

    3.Controller with valid modelstate

    [Test]
    public void test_controller_with_valid_model()
    {
        var controller = new PosController();
        controller.ModelState.Clear();
    
        ActionResult result = posController.Index(new PosViewModel());
    
        // Assert that the controller executed the right actions when the model is valid
    }
    
    0 讨论(0)
  • 2020-12-12 18:01

    I found this solution: SO: Validation does not work when I use Validator.TryValidateObject combined with the solution @Kenneth provided:

    [TestMethod]
        public void test_validation()
        {
            var sut = new POSViewModel();
            // Set some properties here
            var context = new ValidationContext(sut, null, null);
            var results = new List<ValidationResult>();
            TypeDescriptor.AddProviderTransparent(new AssociatedMetadataTypeTypeDescriptionProvider(typeof(POSViewModel), typeof(POSViewModel)), typeof(POSViewModel));
    
            var isModelStateValid = Validator.TryValidateObject(sut, context, results, true);
    
            // Assert here
        }
    

    If you have a class library with all you resources in, don't forget to reference it in your test project.

    0 讨论(0)
提交回复
热议问题