// Because instead of an exception, the result should return. public IEnumerable <ValidationResult> Validate(object source, IMethodAccessor method) { var validationResults = new List <ValidationResult>(); if (method == null) { return(validationResults); } try { // Gets invariant method result. var methodResult = method.Invoke(source); // Prepare validation result. methodResultDispatcher.DispatchResult(methodResult, method.InvariantName, validationResults); } catch (Exception exception) { // Prepare result from exception. methodResultDispatcher.DispatchResult(method.MethodName, exception, validationResults); } return(validationResults); }
public void Should_create_MethodAccessor_for_method() { // Arrange var method = new ClassWithMethods().Method("InvariantMethod"); // Act IMethodAccessor accessor = sut.Create(method); // Assert accessor.Should().NotBeNull(); }
private object FormatValue(AuditEntryState state, string name, object value) { if (value == null) { return(null); } var valueType = value.GetType(); try { object returnValue = valueType.IsEnum ? Enum.GetName(valueType, value) : value; IMethodAccessor formatMethod = Configuration.GetFormatter(state.ObjectType, name); if (formatMethod == null) { return(returnValue); } var context = new AuditPropertyContext { ValueType = valueType, Entity = state.Entity, Value = returnValue }; try { return(formatMethod.Invoke(null, new[] { context })); } catch { // eat format error? return(returnValue); } } catch (Exception ex) { Trace.TraceError(ex.Message); return(_errorText); } }
public void When_validating_a_any_invariant_methods_Should_return_results(object source, IMethodAccessor methodAccessor, string because, IEnumerable <ValidationResult> expected) { // Arrange, Act var actual = sut.Validate(source, methodAccessor); // Assert actual.Should().BeEquivalentTo(expected, because); }