Skip to content

Instantly share code, notes, and snippets.

@royosherove
Created May 16, 2012 09:22
Show Gist options
  • Save royosherove/2709023 to your computer and use it in GitHub Desktop.
Save royosherove/2709023 to your computer and use it in GitHub Desktop.
Test Driven Validation Logic with Extract & Override
public class Dinner
{
public string Description { get; set; }
public string Title { get; set; }
public DateTime EventDate { get; set; }
public bool IsValid { get { return validate(); }}
private bool validate()
{
return validateTitle()
&& validateDescription()
&& validateEventDate();
}
protected virtual bool validateTitle()
{
return Title.Length>3;
}
protected virtual bool validateDescription()
{
return false;
}
protected virtual bool validateEventDate()
{
return false;
}
}
[TestFixture]
public class DinnerTests
{
[Test]
public void IsValid_TitleLessThan4Chars_ReturnsFalse()
{
TestableDinner dinner = new TestableDinner
{
Title = "123",
DescriptionShould = true,
EventDateShould = true
};
Assert.IsFalse(dinner.IsValid);
}
[Test]
public void IsValid_TitleMoreThan3Chars_ReturnsTrue()
{
TestableDinner dinner = new TestableDinner
{
Title = "1234",
DescriptionShould = true,
EventDateShould = true
};
Assert.IsTrue(dinner.IsValid);
}
}
public class TestableDinner : Dinner
{
public bool? DescriptionShould;
public bool? TitleShould;
public bool? EventDateShould;
protected override bool validateTitle()
{
if (TitleShould!=null)
return TitleShould.Value;
return base.validateTitle();
}
protected override bool validateDescription()
{
if (DescriptionShould!= null)
return DescriptionShould.Value;
return base.validateDescription();
}
protected override bool validateEventDate()
{
if (EventDateShould != null)
return EventDateShould.Value;
return base.validateEventDate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment