Created
May 16, 2012 09:22
-
-
Save royosherove/2709023 to your computer and use it in GitHub Desktop.
Test Driven Validation Logic with Extract & Override
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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