Last active
December 23, 2015 20:29
-
-
Save hjerpbakk/6689407 to your computer and use it in GitHub Desktop.
A unit test that will periodically fail as the current day changes.
From: http://hjerpbakk.com/blog/2013/9/24/the-little-test-that-could-not.html
This file contains hidden or 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
[TestFixture] | |
public class BadUnitTestExample { | |
[Test] | |
public void AgeMustReturnCorrectAge_Bad() { | |
var aPerson = new PersonBad { TimeOfBirth = new DateTime(1983, 9, 25) }; | |
Assert.AreEqual(29, aPerson.Age); | |
} | |
} | |
public class PersonBad { | |
public DateTime TimeOfBirth { get; set; } | |
public int Age { get { | |
var today = DateTime.Today; | |
int age = today.Year - TimeOfBirth.Year; | |
if (TimeOfBirth > today.AddYears(-age)) --age; | |
return age; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment