Advancing and proving Application Quality Using Test-Driven Development (TDD)

Adding Functionality

In previous page we had seen that you don’t get demonstrable code. We know that Index Magazine is going to manage a collection of zero or more instances of the class Magazine, so it stands to reason that we might have an AddMagazine method.

What we’ve seen so far, whilst sound TDD practices, hasn’t resulted in much demonstrable code. We know that MagazineIndex is going to manage a collection of zero or more instances of the class Magazine, so it stands to reason that we might have an AddMagazine method. So let us set about writing the test for AddMagazine.

Listing 4 presents our new test case – remember, we must write our tests before we write any implementation code! I’ve done the simplest thing possible, added CheckAddMagazine that creates an instance of MagazineIndex, adds a magazine name, and then checks that the magazine name was added correctly. Listing 4 is not going to compile – there’s no AddMagazine method defined, nor is there an IsMember function.


public void CheckAddMagazine()
MagazineIndex cbtMagazineIndex;
cbtMagazineIndex = new MagazineIndex();
cbtMagazineIndex.AddMagazine(“Methods & Tools”);
Assert.IsTrue(cbtMagazineIndex.IsMember(“Methods & Tools”), “Magazine was not added to collection!”);
Listing 4: Adding functionality
Doing the simplest thing to Listing 2 will allow our new test in Listing 4 to compile. This means adding the AddMagazine and IsMember methods to the class MagazineIndex. Listing 5 presents MagazineIndex with these modifications.
namespace Win App

Public class Magazine Index

Public void Add Magazine(string Magazine Name)

Public bolo Is Member (string Magazine Name)

Return false;
Listing 5: Doing the simplest thing
With Listing 5 in place, we can re-compile the solution and re-run the test. Prepare yourself for some bad news.
Figure 6: Red bar, test fails
Bad news, the test failed. What do we need to do in order to fix this? What might move us in the right direction would be a change to the AddMagazine and the Is Member methods, and the introduction of a string called Magazine List, this is the simplest thing. Listing 6 presents the updated Magazine Index class.
Public class Magazine Index

Private string Magazine List;
public void AddMagazine(string Magazine Name)

Magazine List = Magazine Name;
public book Is Member(string Magazine Name)

return Magazine List == Magazine Name;
Listing 6: Small steps, tests pass
Figure 7: One magazine, the test passes

Adding Two Magazines

Like all good developers, we will write some test code that exercises adding more than one magazine. Listing 7 presents a new test that does just this.
public void CheckAddTwoMagazines
Magazine Index cbtMagazineIndex;
cbtMagazineIndex = new Magazine IndexcbtMagazineIndex.AddMagazine(“Methods & Tool
cut Magazine Index. Add Magazine The Delphi Magazine
Assert .Is True (cbtMagazineIndex.IsMember (“Methods & Tools”), “M&T was not added to collection!”);
Assert. Is True (cbtMagazineIndex.IsMember (“The Delphi Magazine”), “TDM was not added to collection!”);
Listing 7: Adding two magazines to the collection
Figure 8: Two magazines, the test fails
Our simple approach of using a string to manage the Magazine List has caused a test to fail. That is part of the TDD process – let the tests dictate (drive) which code is developed and how it is developed. In this case, CheckAddTwoMagazines dictates that we need to develop a collection or list of some sort to replace the simple string. The TDD mantra of “red, green, refractor” seems to stand true. It is time to refractor the Add Magazine and Is Member methods.
Namespace Wimp
using System. Collections;
public class Magazine Index

private Array List Magazine List = new Array List
public void AddMagazine(string Magazine Name)

MagazineList.Add (Magazine Name);
public bolo Dismember(string Magazine Name)

return (MagazineList.Contains(Magazine Name));
Listing 8: Introducing an Array List
After the introduction of Listing 8 , re-compiling and re-running the tests reveals Figure 9 and a green bar.
Figure 9: Two magazines and a green bar
Array Lists in .NET languages are a useful mechanism that allows us to manage groups of object references, in our case strings. An Array List has the ability to grow as new object references are added – hence it is the ideal abstraction for managing a collection of magazine titles. Array Lists support useful methods like Add, Insert, Remove, and conveniently for us a Contains method (we have used this in our Is Member implementation

Sandeep Shankar
Sandeep Shankar
Sandeep Shankar is a content writer with Webdhoom, a digital marketing firm, which assists both startups and established businesses to improve online traffic, reach out to target audience, increase sales and build loyal customers through best SEO and SMO services. His articles and blogs are widely read and shared on different online platforms.