SAF(NET) = STEPHEN A. FUQUA operating on the Web since 1995

Stephen is a web developer, Bahá'í, and interfaith activist in St. Paul, Minnesota. He likes to write about religion, social justice, sustainability, science, programming, &c.

September 3, 2009

TDD - Scenario for Red, Green, Refactor

Here’s a really dumb scenario that will illustrate a point about the usefulness of the “red, green, refactor” approach to testing and coding. Here’s the functionality – need to test whether or not a string has a value other than 1. Let’s say I write a method before any tests:

public static bool MyMethod(string value)
{
    return value.Equals("1");
}

And let’s say I write my test:

[Test]
public void t_MyMethod_NotOne()
{
    bool returnValue = false;
    MyMethod("2");
    Assert.IsFalse(returnValue);
}

The test will always pass! I wrote it incorrectly, I forgot to set returnValue = MyMethod(“2”). If instead I had written this test with a “red, green, refactor” mindset, then I would have immediately seen that the test was green when it should be read – and then I would go back and realize that I had written the test incorrectly.

Good related links:

  1. Red-Green-Refactor (random blogger)
  2. Guidelines for Test-Driven Development (Microsoft)
  3. Unit Test Rulz (random forum post)

TrackBack

Comments

Post a comment

Remember personal info?




deprecated

On safnet.com

Other sites managed or developed by S.A.F.

S.A.F. elsewhere on the web

  • LinkedIn
    LinkedIn can actually be useful when looking for prospective hires and business or organizational partners
  • GoodReads
    A fun and relatively-unknown social networking site geared towards one's book list
  • Live Journal
    Mirror of the blog at safnet.com, so that a few LJ friends can more easily read and comment there