Code4IT

The place for .NET enthusiasts, Azure lovers, and backend developers

C# Tip: How to create Unit Tests for Model Validation

2023-10-24 3 min read CSharp Tips
Just a second! 🫷
If you are here, it means that you are a software developer. So, you know that storage, networking, and domain management have a cost .

If you want to support this blog, please ensure that you have disabled the adblocker for this site. I configured Google AdSense to show as few ADS as possible - I don't want to bother you with lots of ads, but I still need to add some to pay for the resources for my site.

Thank you for your understanding.
- Davide

Model validation is fundamental to any project: it brings security and robustness acting as a first shield against an invalid state.

You should then add Unit Tests focused on model validation. In fact, when defining the input model, you should always consider both the valid and, even more, the invalid models, making sure that all the invalid models are rejected.

BDD is a good approach for this scenario, and you can use TDD to implement it gradually.

Okay, but how can you validate that the models and model attributes you defined are correct?

Let’s define a simple model:

public class User
{
    [Required]
    [MinLength(3)]
    public string FirstName { get; set; }

    [Required]
    [MinLength(3)]
    public string LastName { get; set; }

    [Range(18, 100)]
    public int Age { get; set; }
}

Have we defined our model correctly? Are we covering all the edge cases? A well-written Unit Test suite is our best friend here!

We have two choices: we can write Integration Tests to send requests to our system, which is running an in-memory server, and check the response we receive. Or we can use the internal Validator class, the one used by ASP.NET to validate input models, to create slim and fast Unit Tests. Let’s use the second approach.

Here’s a utility method we can use in our tests:

public static IList<ValidationResult> ValidateModel(object model)
{
    var results = new List<ValidationResult>();

    var validationContext = new ValidationContext(model, null, null);

    Validator.TryValidateObject(model, validationContext, results, true);

    if (model is IValidatableObject validatableModel)
       results.AddRange(validatableModel.Validate(validationContext));

    return results;
}

In short, we create a validation context without any external dependency, focused only on the input model: new ValidationContext(model, null, null).

Next, we validate each field by calling TryValidateObject and store the validation results in a list, result.

Finally, if the Model implements the IValidatableObject interface, which exposes the Validate method, we call that Validate() method and store the returned validation errors in the final result list created before.

As you can see, we can handle both validation coming from attributes on the fields, such as [Required], and custom validation defined in the model class’s Validate() method.

Now, we can use this method to verify whether the validation passes and, in case it fails, which errors are returned:

[Test]
public void User_ShouldPassValidation_WhenModelIsValid()
{
    var model = new User { FirstName = "Davide", LastName = "Bellone", Age = 32 };
    var validationResult = ModelValidationHelper.ValidateModel(mode);
    Assert.That(validationResult, Is.Empty);
}

[Test]
public void User_ShouldNotPassValidation_WhenLastNameIsEmpty()
{
    var model = new User { FirstName = "Davide", LastName = null, Age = 32 };
    var validationResult = ModelValidationHelper.ValidateModel(mode);
    Assert.That(validationResult, Is.Not.Empty);
}


[Test]
public void User_ShouldNotPassValidation_WhenAgeIsLessThan18()
{
    var model = new User { FirstName = "Davide", LastName = "Bellone", Age = 10 };
    var validationResult = ModelValidationHelper.ValidateModel(mode);
    Assert.That(validationResult, Is.Not.Empty);
}

Further readings

Model Validation allows you to create more robust APIs. To improve robustness, you can follow Postel’s law:

πŸ”— Postel’s law for API Robustness | Code4IT

This article first appeared on Code4IT 🐧

Model validation, in my opinion, is one of the cases where Unit Tests are way better than Integration Tests. This is a perfect example of Testing Diamond, the best (in most cases) way to structure a test suite:

πŸ”— Testing Pyramid vs Testing Diamond (and how they affect Code Coverage) | Code4IT

If you still prefer writing Integration Tests for this kind of operation, you can rely on the WebApplicationFactory class and use it in your NUnit tests:

πŸ”— Advanced Integration Tests for .NET 7 API with WebApplicationFactory and NUnit | Code4IT

Wrapping up

Model validation is crucial. Testing the correctness of model validation can make or break your application. Please don’t skip it!

I hope you enjoyed this article! Let’s keep in touch on Twitter or LinkedIn! πŸ€œπŸ€›

Happy coding!

🐧

About the author

Davide Bellone is a Principal Backend Developer with more than 10 years of professional experience with Microsoft platforms and frameworks.

He loves learning new things and sharing these learnings with others: that’s why he writes on this blog and is involved as speaker at tech conferences.

He's a Microsoft MVP πŸ†, conference speaker (here's his Sessionize Profile) and content creator on LinkedIn.