Unit testing model validation in ASP.NET Core
This post shows an example of unit testing an ASP.NET Core model validation property. [RegularExpression]
enforces the property matches the regex:
Using the attribute is straightforward but the APIs I ran into to write my unit tests were somewhat more confusing. This is what I settled on doing:
If the MemberName
property is not set on the ValidationContext
, TryValidateProperty
will throw a ArgumentNullException
.