Reputation: 4552
If I have code like the following:
public const string UNSPECIFIED_DATATYPE = "11";
private string SelectedValue = "11";
public bool Validate(object sender, EventArgs eventArgs)
{
return IsValid();
}
private bool IsValid()
{
return (SelectedValue != UNSPECIFIED_DATATYPE);
}
The method signature is actually for an ASP.Net CustomValidatorControl. I have no scope to change this implementation.
SelectedValue will come from a list control and I want to at least test a positive and negative condition.
Ideally, something like:
public void Test_When_SelectedValue_IS_UnSPecified_validate_Returns_False
{
Assert.IsFalse(Validate(UNSPECIFIED_DATATYPE));
}
Obviously, this is not possible due to the method signature of Validate().
How do I write clean and meaningful unit tests to check that the public Validate method with different simulated selected Values?
Thanks
Upvotes: 3
Views: 830
Reputation: 4114
IsValid this method is private and not so good to test you can extract validation logic in separate class, and then delegate in CustomValidatorControl validation logic into this class. This is help you to test all logic in this class look at the following example
public class SelectedValueValidator
{
public const string UNSPECIFIED_DATATYPE = "11";
private string selectedValue = "11";
public string SelectedValue
{
get { return selectedValue; }
set { selectedValue = value; }
}
public bool IsValid()
{
return (SelectedValue != UNSPECIFIED_DATATYPE);
}
}
and ther is tests for this class
[Test]
public void IsValid_SelectedValueDifferent_ReturnTrue()
{
//Arrange
var validator = new SelectedValueValidator { SelectedValue = "123" };
//Act
bool result = validator.IsValid();
//Assert
Assert.That(result, Is.True);
}
[Test]
public void IsValid_SelectedValueIsTheSame_ReturnFalse()
{
//Arrange
var validator = new SelectedValueValidator ();
//Act
bool result = validator.IsValid();
//Assert
Assert.That(result, Is.False);
}
and the in method Validate you should write
public bool Validate(object sender, EventArgs eventArgs)
{
return validator.IsValid();
}
SelectedValue also you sould map into Validator class
Upvotes: 2
Reputation: 46008
SelectedValue
to different valuesIsValid
You may need to use a bit of reflection...
Upvotes: 0