/ / ASP.Net MVC: सिस्टम कंपोनोड्मल डेटाैनोटेशंस वैल्यू शून्य नहीं हो सकता है - asp.net-mvc, यूनिट-टेस्टिंग, डेटा-एनोटेशन, बनाम यूनिट-टेस्टिंग-फ्रेमवर्क

ASP.Net MVC: सिस्टम कंपोनोड्मल डेटाैनोटेशंस वैल्यू शून्य नहीं हो सकता है - asp.net-mvc, यूनिट-टेस्टिंग, डेटा-एनोटेशन, बनाम यूनिट-टेस्टिंग-फ्रेमवर्क

जब मैं इकाई परीक्षण कर रहा हूँ तो मेरा कस्टम सत्यापन तर्क है तो मुझे यह त्रुटि मिल रही है

"System.ArgumentNullException" प्रकार का एक अपवाद इसमें हुआ System.ComponentModel.DataAnnotations.dll लेकिन उपयोगकर्ता में संभाला नहीं गया था कोड

अतिरिक्त जानकारी: मान शून्य नहीं हो सकता।

यहाँ मेरा पूरा कोड है। तो कृपया देखें और मुझे बताएं कि मैंने कहाँ गलती की है?

मेरा मॉडल कोड

public class DateValTest
{
[Display(Name = "Start Date")]
[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? StartDate { get; set; }

[Display(Name = "End Date")]
[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
[MyDate(ErrorMessage = "Back date entry not allowed")]
[DateGreaterThanAttribute(otherPropertyName = "StartDate", ErrorMessage = "End date must be greater than start date")]
public DateTime?  EndDate { get; set; }
}

यहाँ मेरा कस्टम सत्यापन कोड है

public class MyDateAttribute : ValidationAttribute, IClientValidatable
{
public DateTime _MinDate;

public MyDateAttribute()
{
_MinDate = DateTime.Today;
}

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
DateTime _EndDat = DateTime.Parse(value.ToString(), CultureInfo.InvariantCulture);
DateTime _CurDate = DateTime.Today;

int cmp = _EndDat.CompareTo(_CurDate);
if (cmp > 0)
{
// date1 is greater means date1 is comes after date2
return ValidationResult.Success;
}
else if (cmp < 0)
{
// date2 is greater means date1 is comes after date1
return new ValidationResult(ErrorMessage);
}
else
{
// date1 is same as date2
return ValidationResult.Success;
}
}

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
ValidationType = "restrictbackdates",
};
rule.ValidationParameters.Add("mindate", _MinDate);
yield return rule;
}
}

मेरी इकाई परीक्षण कोड जो त्रुटि फेंक रहा है

[TestClass]
public class MyDateAttribute_Test
{
[TestMethod]
[ExpectedException(typeof(ValidationException))]
public void Test_EndDateIsInvalidIFBackDate()
{
var validationResults = new List<ValidationResult>();
DateValTest model = new DateValTest() { EndDate = DateTime.Today.AddDays(10) };
ValidationContext context = new ValidationContext(model);
Validator.TryValidateProperty(model, context, validationResults);
Assert.IsTrue(validationResults.Count > 0);
}
}

उस क्षेत्र को उजागर करें जहां मैंने गलती की। धन्यवाद

उत्तर:

जवाब के लिए 0 № 1

मैं इसे इस तरह से हल करता हूं।

[TestMethod]
//[ExpectedException(typeof(ValidationException))]
public void Test_EndDateIsInvalidIFBackDate()
{
var validationResults = new List<ValidationResult>();
DateValTest model = new DateValTest() { StartDate = DateTime.Today, EndDate = DateTime.Today.AddDays(10) };
ValidationContext context = new ValidationContext(model);
MyDateAttribute attribute = new MyDateAttribute();
//attribute.Validate(model.EndDate, context);
Validator.TryValidateObject(model, context, validationResults, true);
//Assert.IsFalse(validationResults.Count > 0);
Assert.AreEqual(false, (validationResults.Count > 0), "Test Equalateral");
//Assert.IsTrue(validationResults.Any(vr => vr.ErrorMessage == "Back date entry not allowed"));
}