I was trying to do unit testing for validations on c# models using Validator but it is not working with NewtonSoft attributes.
Here's my unit test:
var searchController = new SearchController();
var requestModel = new RequestModel
{
Id = "23456",
MobileNumber = "9876543210"
};
var results = new List<ValidationResult>();
var context = new ValidationContext(requestModel, null, null);
var isvalid = Validator.TryValidateObject(requestModel, context, results,true);
Assert.IsFalse(isvalid);
And here's my model:
public class XYZ
{
[JsonProperty("mobileNumber")]
[MinLength(3, ErrorMessage = "MOBILENUMBER_LENGTH_MIN")]
public string MobileNumber { get; set; }
[JsonProperty("refNumber")]
[MinLength(3, ErrorMessage = "REFNUMBER_LENGTH_MIN")]
public string RefNumber { get; set; }
[JsonProperty("subscriberFirstName")]
[MinLength(3, ErrorMessage = "FIRSTNAME_LENGTH_MIN")]
public string FirstName { get; set; }
}
Can someone help me with this?