3

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

davy
  • 4,474
  • 10
  • 48
  • 71

2 Answers2

2

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

Sergey K
  • 4,071
  • 2
  • 23
  • 34
  • Thanks Serghei. I like this approach as it can be applied to many similar situations, which is what I was looking for. – davy Nov 30 '11 at 11:26
  • Yes, @davy you are right this is general aproach I use it wery often – Sergey K Nov 30 '11 at 11:30
  • @Serghie, I can't try this right now as I'm not at my dev machine. Will the public Validate method be covered by test coverage tools? – davy Nov 30 '11 at 11:31
  • Validate method I think not, but you can write for this method test where you just need to verify that the validator.IsValid() method was called, because this Validate method doesn't have any additional logic. Any way try it. – Sergey K Nov 30 '11 at 11:38
0
  1. Set SelectedValue to different values
  2. Call IsValid

You may need to use a bit of reflection...

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126