Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit test of ValidationContext is failing?
    text
    copied!<p>I am trying to unit test validations on a object with a base class. I am expecting 3 properties (Email,Phone and Skill>0) to fail validation, but the test is failing.</p> <p>BASE CLASS</p> <pre><code>public abstract class Person : Entity { public string FirstName { get; set; } public string LastName { get; set; } public string UserName { get; set; } public string FullName { get { return string.Format("{0} {1}", FirstName, LastName); } } public string Email { get; set; } public string Phone { get; set; } public DateTime DateAdded { get; set; } public bool Active { get; set; } } </code></pre> <p>DERIVED CLASS</p> <pre><code> public class Contractor : Person, IValidatableObject { public Address Address { get; set; } public List&lt;Skill&gt; SkillSet { get; set; } public DateTime? NextAvailableDate { get; set; } public Contractor() { } public IEnumerable&lt;ValidationResult&gt; Validate(ValidationContext validationContext) { if (string.IsNullOrWhiteSpace(base.FirstName)) yield return new ValidationResult("The First Name field cannot be empty."); if (string.IsNullOrWhiteSpace(base.LastName)) yield return new ValidationResult("The Last Name field cannot be empty."); if (string.IsNullOrWhiteSpace(base.Email)) yield return new ValidationResult("The Email field cannot be empty."); if (string.IsNullOrWhiteSpace(base.Phone)) yield return new ValidationResult("The Phone field cannot be empty."); if (SkillSet.Count() &lt; 1) yield return new ValidationResult("A contractor must have at least one skill."); } </code></pre> <p>TEST</p> <pre><code>[TestMethod] public void contractor_is_missing_email_phone_skill_when_created() { //Arrange Contractor contractor = new Contractor { FirstName = "abc", LastName = "def" }; //Act ValidationContext context = new ValidationContext(contractor); IEnumerable&lt;ValidationResult&gt; result = contractor.Validate(new ValidationContext(contractor)); List&lt;ValidationResult&gt; r = new List&lt;ValidationResult&gt;(result); //Assert Assert.IsTrue(r.Count == 3); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload