public void Validate_Does_Not_Fail_If_The_PropertyInfo_Parameter_Is_Null() { var validator = new AggregateValidator(new Dictionary<Type, IValueValidator>()); string result; validator.Validate("value", "name", null, out result); }
public void Validate_Calls_Property_Validator_Attribute() { const string value = "value"; const string name = "name"; var result = value; var validatorMockAttribute = new Mock <Attribute>(); var validatorMock = validatorMockAttribute.As <IValueValidator>(); validatorMock.Setup(v => v.Validate(value, name, It.IsAny <ICustomAttributeProvider>(), out result)) .Returns(true); var ICustomAttributeProviderMock = new Mock <ICustomAttributeProvider>(); ICustomAttributeProviderMock.Setup(p => p.GetCustomAttributes(true)) .Returns(() => new object[] { validatorMockAttribute.Object }); var validator = new AggregateValidator(new Dictionary <Type, IValueValidator>()); var coerced = validator.Validate(value, name, ICustomAttributeProviderMock.Object, out result); ICustomAttributeProviderMock.Verify(p => p.GetCustomAttributes(true), Times.Once); validatorMock.Verify(v => v.Validate(value, name, ICustomAttributeProviderMock.Object, out result), Times.Once); Assert.AreEqual(value, result); Assert.IsTrue(coerced); }
public void Validate_Does_Not_Fail_If_The_ICustomAttributeProvider_Parameter_Is_Null() { var validator = new AggregateValidator(new Dictionary <Type, IValueValidator>()); string result; validator.Validate("value", "name", null, out result); }
public void TestSumSimple() { var testData = new[] { 1, 2, 3 }; var test = new AggregateValidator <int>((collection) => collection.Sum(), (sum) => sum == 6); Assert.IsTrue(test.Validate(testData).IsValid); }
public void TestSumSimple() { var testData = new[] { 1, 2, 3 }; var test = new AggregateValidator<int>((collection) => collection.Sum(), (sum) => sum == 6); Assert.IsTrue(test.Validate(testData).IsValid); }
public void TestSumWithError() { var testData = new[] { new GenericParameter { ID = 1 }, new GenericParameter { ID = 2 }, new GenericParameter { ID = 3 } }; var test = new AggregateValidator<GenericParameter, int>((p) => p.ID, (collection) => collection.Sum(), (sum) => sum == 5, ErrorMessageText); var result = test.Validate(testData); Assert.IsFalse(result.IsValid); Assert.IsTrue(result.Count() == 1); Assert.IsTrue(result.ElementAt(0) is AggregateValidationResult<int>); Assert.AreEqual(((AggregateValidationResult<int>)result.ElementAt(0)).ActualValue, 6); Assert.AreEqual(result.ElementAt(0).ErrorMessage, ErrorMessageText); }
public void Validate_AllCallsSucceed_ReturnsNull() { var mockVal1 = new Mock<IValidator>(); var mockVal2 = new Mock<IValidator>(); var sub = new AggregateValidator(); sub.Add(mockVal1.Object); sub.Add(mockVal2.Object); var result = sub.Validate(new MockUserAccountService().Object, new MockUserAccount().Object, "test"); Assert.IsNull(result); }
public void Validate_GetsErrorFromItems() { var mockVal = new Mock<IValidator>(); var error = new ValidationResult("Error"); mockVal.Setup(x => x.Validate(It.IsAny<UserAccountService>(), It.IsAny<UserAccount>(), It.IsAny<string>())).Returns(error); var sub = new AggregateValidator(); sub.Add(mockVal.Object); var result = sub.Validate(new MockUserAccountService().Object, new MockUserAccount().Object, "test"); Assert.AreSame(error, result); }
public void Validate_NullAccount_Throws() { var sub = new AggregateValidator(); try { var result = sub.Validate(new MockUserAccountService().Object, null, "test"); Assert.Fail(); } catch (ArgumentException) { } }
public void TestSumWithSelector() { var testData = new[] { new GenericParameter { ID = 1 }, new GenericParameter { ID = 2 }, new GenericParameter { ID = 3 } }; var test = new AggregateValidator <GenericParameter, int>((p) => p.ID, (collection) => collection.Sum(), (sum) => sum == 6); Assert.IsTrue(test.Validate(testData).IsValid); }
public void Validate_AllCallsSucceed_ReturnsNull() { var mockVal1 = new Mock <IValidator>(); var mockVal2 = new Mock <IValidator>(); var sub = new AggregateValidator(); sub.Add(mockVal1.Object); sub.Add(mockVal2.Object); var result = sub.Validate(new MockUserAccountService().Object, new MockUserAccount().Object, "test"); Assert.IsNull(result); }
public void Validate_GetsErrorFromItems() { var mockVal = new Mock <IValidator>(); var error = new ValidationResult("Error"); mockVal.Setup(x => x.Validate(It.IsAny <UserAccountService>(), It.IsAny <UserAccount>(), It.IsAny <string>())).Returns(error); var sub = new AggregateValidator(); sub.Add(mockVal.Object); var result = sub.Validate(new MockUserAccountService().Object, new MockUserAccount().Object, "test"); Assert.AreSame(error, result); }
public void Validate_AllCallsSucceed_CallsAllValidators() { var mockVal1 = new Mock<IValidator>(); var mockVal2 = new Mock<IValidator>(); var sub = new AggregateValidator(); sub.Add(mockVal1.Object); sub.Add(mockVal2.Object); var svc = new MockUserAccountService(); var ua = new MockUserAccount(); var val = "test"; var result = sub.Validate(svc.Object, ua.Object, val); mockVal1.Verify(x=>x.Validate(svc.Object, ua.Object, val)); mockVal2.Verify(x=>x.Validate(svc.Object, ua.Object, val)); }
public void Validate_AllCallsSucceed_CallsAllValidators() { var mockVal1 = new Mock <IValidator>(); var mockVal2 = new Mock <IValidator>(); var sub = new AggregateValidator(); sub.Add(mockVal1.Object); sub.Add(mockVal2.Object); var svc = new MockUserAccountService(); var ua = new MockUserAccount(); var val = "test"; var result = sub.Validate(svc.Object, ua.Object, val); mockVal1.Verify(x => x.Validate(svc.Object, ua.Object, val)); mockVal2.Verify(x => x.Validate(svc.Object, ua.Object, val)); }
public void TestSumWithError() { var testData = new[] { new GenericParameter { ID = 1 }, new GenericParameter { ID = 2 }, new GenericParameter { ID = 3 } }; var test = new AggregateValidator <GenericParameter, int>((p) => p.ID, (collection) => collection.Sum(), (sum) => sum == 5, ErrorMessageText); var result = test.Validate(testData); Assert.IsFalse(result.IsValid); Assert.IsTrue(result.Count() == 1); Assert.IsTrue(result.ElementAt(0) is AggregateValidationResult <int>); Assert.AreEqual(((AggregateValidationResult <int>)result.ElementAt(0)).ActualValue, 6); Assert.AreEqual(result.ElementAt(0).ErrorMessage, ErrorMessageText); }
public void Validate_Calls_Type_Validator() { const string value = "value"; const string name = "name"; string result; var validatorMock = new Mock<IValueValidator>(); validatorMock.Setup(v => v.Validate(value, name, null, out result)); var validator = new AggregateValidator(new Dictionary<Type, IValueValidator> { [typeof(string)] = validatorMock.Object }); validator.Validate(value, name, null, out result); validatorMock.Verify(v => v.Validate(value, name, null, out result), Times.Once); }
public void Validate_Calls_Type_Validator() { const string value = "value"; const string name = "name"; string result; var validatorMock = new Mock <IValueValidator>(); validatorMock.Setup(v => v.Validate(value, name, null, out result)); var validator = new AggregateValidator(new Dictionary <Type, IValueValidator> { [typeof(string)] = validatorMock.Object }); validator.Validate(value, name, null, out result); validatorMock.Verify(v => v.Validate(value, name, null, out result), Times.Once); }
public void Validate_Calls_Property_Validator_Attribute() { const string value = "value"; const string name = "name"; var result = value; var validatorMock = new Mock<IValueValidator>(); validatorMock.Setup(v => v.Validate(value, name, It.IsAny<PropertyInfo>(), out result)) .Returns(true); var propertyInfoMock = new Mock<PropertyInfo>(); propertyInfoMock.Setup(p => p.GetCustomAttributes(true)) .Returns(() => new object[] { validatorMock.Object }); var validator = new AggregateValidator(new Dictionary<Type, IValueValidator>()); var coerced = validator.Validate(value, name, propertyInfoMock.Object, out result); propertyInfoMock.Verify(p => p.GetCustomAttributes(true), Times.Once); validatorMock.Verify(v => v.Validate(value, name, propertyInfoMock.Object, out result), Times.Once); Assert.AreEqual(value, result); Assert.IsTrue(coerced); }
public void TestSumWithSelector() { var testData = new[] { new GenericParameter { ID = 1 }, new GenericParameter { ID = 2 }, new GenericParameter { ID = 3 } }; var test = new AggregateValidator<GenericParameter, int>((p) => p.ID, (collection) => collection.Sum(), (sum) => sum == 6); Assert.IsTrue(test.Validate(testData).IsValid); }