Skip to content

Instantly share code, notes, and snippets.

@christopherbauer
Created October 6, 2015 14:42
Show Gist options
  • Select an option

  • Save christopherbauer/085057f6423687c6efb8 to your computer and use it in GitHub Desktop.

Select an option

Save christopherbauer/085057f6423687c6efb8 to your computer and use it in GitHub Desktop.
public class RequiredIfAttributeTests
{
[TestFixture]
public class when_validating_required_if_attribute
{
private class TestModel
{
[RequiredIf("Property2", true)]
public int? Property1 { get; set; }
public bool Property2 { get; set; }
}
[Test]
public void then_should_fail_model_validation_given_validation_property_null_and_target_property_value_equal_to_expected_value()
{
// Arrange
var model = new TestModel { Property2 = true, Property1 = null };
// Act
var result = Validator.TryValidateObject(model, new ValidationContext(model, null, null), null, true);
// Assert
Assert.That(result, Is.False);
}
[Test]
public void then_should_succeed_model_validation_given_validation_property_null_and_target_property_value_not_equal_to_expected_value()
{
// Arrange
var model = new TestModel { Property2 = false, Property1 = null };
// Act
var result = Validator.TryValidateObject(model, new ValidationContext(model, null, null), null, true);
// Assert
Assert.That(result, Is.True);
}
[Test]
public void then_should_fail_model_validation_given_validation_property_has_value_and_target_property_value_not_equal_to_expected_value()
{
// Arrange
var model = new TestModel { Property2 = true, Property1 = 1 };
// Act
var result = Validator.TryValidateObject(model, new ValidationContext(model, null, null), null, true);
// Assert
Assert.That(result, Is.True);
}
[Test]
public void then_should_succeed_model_validation_given_validation_property_has_value_and_target_property_value_equal_to_expected_value()
{
// Arrange
var model = new TestModel { Property2 = true, Property1 = 1 };
// Act
var result = Validator.TryValidateObject(model, new ValidationContext(model, null, null), null, true);
// Assert
Assert.That(result, Is.True);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment