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.