1 回答

TA貢獻1812條經驗 獲得超5個贊
using System.ComponentModel.DataAnnotations;
/*other using*/
[Fact]
public void UserValidationError()
{
? ? //Arrange
? ? BaseUserDTO userDTO = new BaseUserDTO
? ? {
? ? ? ?FirstName = "222A@@@",
? ? ? ?LastName = "Test",
? ? ? ?Email = "[email protected]",
? ? ? ?PhoneNumber = "(111)111-1111",
? ? ? ?Role = 0,
? ? ? ?Password = "1234567A",
? ? ? ?RetypePassword = "1234567A"
? ? };
? ? //ACT
? ? var lstErrors = ValidateModel(userDTO);
? ? //ASSERT
? ? Assert.IsTrue(lstErrors.Count == 1);? ?
? ? //Or?
? ? Assert.IsTrue(lstErrors.Where(x => x.ErrorMessage.Contains("Use only Latin characters")).Count() > 0);
}
//http://stackoverflow.com/questions/2167811/unit-testing-asp-net-dataannotations-validation
? ? ? ? private IList<ValidationResult> ValidateModel(object model)
? ? ? ? {
? ? ? ? ? ? var validationResults = new List<ValidationResult>();
? ? ? ? ? ? var ctx = new ValidationContext(model, null, null);
? ? ? ? ? ? Validator.TryValidateObject(model, ctx, validationResults, true);
? ? ? ? ? ? return validationResults;
? ? ? ? }
- 1 回答
- 0 關注
- 141 瀏覽
添加回答
舉報