2

The scenario I have is to Draw the specified number of cards from the deck and return them in sorted order (first by suit, then by face value).

<param name="howMany">how many cards to draw.
<returns>a list of drawn cards, sorted by suit and face value (rank)</returns>

I have coded this Method to achieve it.I have to write an Unit Test code for it. what can be possible unit test for this as It has a list to compare and how to achieve that.

public List<Card> DrawSorted(int howMany)
{
    List<Card> drawnCards = _cards.Take(howMany).ToList();
    IEnumerable<Card> sortedCards = from card in drawnCards
                                            orderby card.Order ascending
                                            select card;
    return sortedCards.ToList();
}

Any help is much appreciated

3 Answers 3

2

You should make tests as simple as possible, and should probably make several tests here:

Some rough ideas:

  • One to verify that you draw and return the correct number of cards
  • One to check that if you draw cards that you know are in the correct order to begin with, you will still get them back in the correct order
  • One to check that cards that are NOT in the correct order will be returned in the correct order.

A simple way to check ordering is by just drawing a small umber of cards (maybe even just two?), and comparing those; that will let you keep the test-logic as short and simple as possible, while still being able to give a warning if something is wrong.

Of course the better process would have been to specify your most simple requirements in the form of tests to begin with, and then make them more advanced bit by bit, while adding logic to your method as you go. You might then have tests named something like this (you can probably guess what they might contain):

public void should_return_correct_nr_of_cards(){ ... }

public void should_return_presorted_cards_in_correct_order(){ ... }

public void should_return_unsorted_cards_sorted(){ ... }
2
  • Actually it's not a problem to have a sorted deck of cards, and then compare results with first N cards from this sorted deck. Also I would rename this method, because it does not draw cards. And I would add tests for verifying behavior with incorrect howMany value (zero, less than zero, more then deck size) Feb 21, 2014 at 7:47
  • Good points. Although the method name does not state that it was testing the drawing, strictly speaking, I've still renamed it now to make it a little simpler and less likely to be misunderstood. This is a beginning; I suppose you could add more tests, e.g. to test for various potential mistakes, but I would not want to overdo it either. You should not feel that you absolutely have to test for every possible eventuality - just enough that you can feel reasonably confident about your code.
    – Kjartan
    Feb 21, 2014 at 7:59
1

I hope this helps!

I think that one possible unit test is to check if the result is already sorted after your DrawSorted is called.

I would create other method called IsSorted, returning a bool, and then use this method to test the output.

Something like:

bool expectedAnswer = true;

List<Cards> list = yourClass.DrawSorted(15);
bool result = yourClass.IsSorted(list);

Assert.AreEqual(expectedResult, result);

Or something like this.

1

The first question you have to ask is "What do I want to test?". Are you testing that all the cards are different? Are you testing what happens when you draw more cards than there are? Myself I'd test both (and a few other edge case scenarios).

The second thing you have to do is provide some mechanism for mocking the list of cards to draw from. For a test to be valuable you need to be able set the conditions of the test each time.

My suggestion would be to create your test class like so:

public class Deck
{
  private List<Card> _cards;

  public Deck(List<Card> allCards)
  {
    _cards = allCards;
  }

  public List<Card> DrawSorted(int howMany)
  {
    List<Card> drawnCards = _cards.Take(howMany).ToList();
    IEnumerable<Card> sortedCards = from card in drawnCards
                                            orderby card.Order ascending
                                            select card;
    return sortedCards.ToList();
  }
}

In your test class you could pass in the complete list of cards you want to use for your test.

Depending on the test framework you're using the tests themselves will be slightly different. But here's an example:

[Test]
public void CheckCorrectNumberOfCardsAreReturned()
{
  List<Card> allCards = GenerateDeck();
  var deck = new Deck(allCards);
  var drawnCards = deck.DrawSorted(5);
  Assert.AreEqual(5, drawnCards.Count());
}

For your testing to be truly valuable what happens if you try and draw 55 cards? Should you get 55 or 52 (assuming a standard deck of cards)?

If I have duplicates in my allCards should I get duplicates when I draw them or should some be removed?

Testing is tied directly into requirements - work out what your method should do and design your tests to ensure your code meets them.

A couple of final points:

  • One Test should test one edge case/piece of behavior, this often results in multiple tests per method
  • A test should have known starting conditions (you can configure your deck to give the test conditions you want)
  • A test should be repeatable, be wary of things like random numbers. Draw a random card from two deck and check they're not the same? 1/52 they could be! These sort of tests are a nightmare to diagnose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.