Share via


Bookmark Collection: Implementing the Add Method

The first test I implemented last week turned out to describe an excellent example of the Fake It ('Til You Make It') pattern. As we continue with the next test let's see if we will continue to "Fake It" or do we have enough tests and information to begin formulating a solution. The next test to be implemented is "Add a Bookmark(label, Uri), Count == 1". Let's write this test:

[Test]
public void AddBookmarkCountIsIncremented()
{
BookmarkCollection collection = new BookmarkCollection();
collection.Add("Label", new Uri(https://example.com));
Assert.AreEqual(1, collection.Count);
}

When I compile this test, the compiler graciously tells me that the Add method is not implemented in the BookmarkCollection class. Using the "Fake It" implementation that Paul Bartlett pointed out last week here is the first implementation of the Add method.

public void Add(string label, Uri site)
{
throw new NotImplementedException(
"Fake It ('Til You Make It')");
}

This also happens to be the implementation that you get when you rely on Visual Studio to generate the method stub (a new feature in VS 2005) sans the message. When this code is compiled and runs the test fails as expected. Let's implement something that will make the two tests pass. One thing to keep in mind is that we want the simplest implementation that is needed to make the both tests pass. Here is my solution (at least for now): 

public

class BookmarkCollection
{
private int count;

public int Count
{
get { return count; }
}

public void Add(string label, Uri site)
{
count++;
}
}

When I compile and run this code both tests pass. We are done; for now. I am not too concerned that we have not gotten to the essance of the problem as of yet or that the implementation that we have is not something that will last. So far, we have focused on the interface and flushed out a couple of the methods. The next few tests will flush out the best implementation. As the implementation is modified we have the existing tests in place to make sure the solution is progressing overall.

As a side note, I am not sure if I will show each test as a blog entry. At times they do get repetitive. Let me know if you feel strongly, one way or the other.

Comments

  • Anonymous
    November 15, 2004
    Slight typo in my blog link (; -> l), but thanks for the attribution :)
  • Anonymous
    November 15, 2004
    Just show however many tests make a complete blog post. If you need to review some of the older tests, go ahead. If not, skip em!
  • Anonymous
    November 16, 2004
    Sorry about that Paul. The link has been corrected.
  • Anonymous
    November 17, 2004
    I'm quite new to TDD and am very interested in seeing the mental reasoning behind what your doing. I like being able to see that in each blog entry. If there is another way that doesn't degrade the thought process then by all means go with it. Better communicate too much than not enough.