Beginning Test Driven Development
page 4 of 7
by Brendan Enrick
Feedback
Average Rating: 
Views (Total / Last 10 Days): 35032/ 72

Making the Tests Pass

I do not want to spend a lot of time talking about our implementation which makes the tests pass, because it is not the focus of the article. For now I will just include a quick solution which causes the tests to pass. Keep in mind that at each step we should only write just enough to make the tests pass. We will write more tests in a moment which help us create more functionality. If we flesh out the Deck class with code that actually works, we end up with something similar to the following code.

Listing 2: The Simple Implementation

public class Deck
{
    private readonly List<Card> _cards;
 
    public Deck(List<Card> cards)
    {
        _cards = cards;
    }
 
    public int Count 
    {
        get { return _cards.Count; }
    }
 
    public Card DrawCard()
    {
        Card drawnCard = _cards[0];
        _cards.Remove(drawnCard);
        return drawnCard;
    }
}

Notice here that there are still plenty of bugs which can occur with this code. For example if the deck is empty, I am going to get an ArgumentOutOfRangeException. We need to get some more tests in place and make them pass.


View Entire Article

User Comments

No comments posted yet.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-02-25 7:11:23 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search