public void GiveACommunicationWithAnUnknownType_Validate_ShouldHaveAValidationError() { // Arrange. var listingAgent = new ListingAgent { Name = "a", Communications = new List <Communication> { new Communication { CommunicationType = CommunicationType.Unknown, Details = "a" } } }; // Act. _validator.ShouldHaveChildValidator(agent => agent.Communications, typeof(CommunicationValidator)); var result = _validator.Validate(listingAgent); //_validator.ShouldNotHaveValidationErrorFor(agent => agent.Communications, new[] {communication}); // Assert. result.Errors.ShouldContain(x => x.PropertyName == "Communications[0].CommunicationType"); }
public void GiveSomeCommunication_Validate_ShouldNotHaveAValidationError() { // Arrange. var listingAgent = new ListingAgent { Name = "a", Communications = new List <Communication> { new Communication { CommunicationType = CommunicationType.Email, Details = "a" } } }; // Act. _validator.ShouldHaveChildValidator(agent => agent.Communications, typeof(CommunicationValidator)); var result = _validator.Validate(listingAgent); //_validator.ShouldNotHaveValidationErrorFor(agent => agent.Communications, new[] {communication}); // Assert. result.Errors.Count.ShouldBe(0); }
private static IList <ListingAgent> ExtractAgent(XElement document) { document.ShouldNotBe(null); var agentElements = document.Elements("listingAgent").ToArray(); if (!agentElements.Any()) { return(null); } var agents = new List <ListingAgent>(); foreach (var agentElement in agentElements) { var agent = new ListingAgent { Name = agentElement.ValueOrDefault("name") }; var orderValue = agentElement.AttributeValueOrDefault("id"); int order = 0; if (!string.IsNullOrWhiteSpace(orderValue) && int.TryParse(orderValue, out order)) { agent.Order = order; } var email = agentElement.ValueOrDefault("email"); agent.Communications = new List <Communication>(); if (!string.IsNullOrWhiteSpace(email)) { agent.Communications.Add(new Communication { CommunicationType = CommunicationType.Email, Details = email }); } var phoneMobile = agentElement.ValueOrDefault("telephone", "type", "mobile"); if (!string.IsNullOrWhiteSpace(phoneMobile)) { agent.Communications.Add(new Communication { CommunicationType = CommunicationType.Mobile, Details = phoneMobile }); } var phoneOffice = agentElement.ValueOrDefault("telephone", "type", "BH"); if (!string.IsNullOrWhiteSpace(phoneOffice)) { agent.Communications.Add(new Communication { CommunicationType = CommunicationType.Landline, Details = phoneOffice }); } // Some listings have this element but no data provided. :( // So we don't add 'emtpy' agents. if (!string.IsNullOrWhiteSpace(agent.Name) && agent.Communications.Any()) { agents.Add(agent); } } var counter = 0; return(agents.Any() ? agents .OrderBy(x => x.Order) .Select(x => new ListingAgent { Name = x.Name, Order = ++counter, Communications = x.Communications }) .ToList() : null); }