Skip to content

Instantly share code, notes, and snippets.

@hjerpbakk
Last active December 23, 2015 20:29
Show Gist options
  • Save hjerpbakk/6689407 to your computer and use it in GitHub Desktop.
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
[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