First off, I'm using the NUnit.framework in c# to run tests. I'm building a card game (the game Dominion) in c#. I'm trying to writer a unit test that tests that when I shuffle my discard pile into my deck, the new shuffled deck contains all the same cards.
I'm getting an error in a helper method I wrote to compare if 2 dictionaries representing the count of the cards are equivalent. (Dictionary<Card, int>
, where Card
is a custom class that is hashed by an id field each instance has, and int
being how many of that card a deck contains.
Right now, I'm getting a NullReferenceException
on the line between the prints "before" and "after". Here's the helper method:
this code is called from within an Assert.True
statement, which is the line that NUnit reports as having the error, but the print statement "before" is the last thing to print to the console in the first loop (the loop should run twice).
I'm new to c# and I don't really understand how this works, so I could use some help.
[edit] added unit test code.
[Test()]
public void testShuffledDeckContainsSameCards()
{
ArrayList list = new ArrayList();
Stack<Card> shuffledDeck;
Dictionary<Card, int> cardCount;
Dictionary<Card, int> expect = new Dictionary<Card,int>();
for (int i = 0; i < 7; i++)
{
list.Add(new KingdomCards.Copper());
}
expect.Add(new KingdomCards.Copper(), 7);
shuffledDeck = (Stack<Card>) Player.Shuffle(list);
cardCount = countCards(shuffledDeck);
//CompareCounts(expect, cardCount);
list.Add(new KingdomCards.Estate());
list.Add(new KingdomCards.Estate());
list.Add(new KingdomCards.Estate());
expect.Add(new KingdomCards.Estate(), 3);
shuffledDeck = (Stack<Card>) Player.Shuffle(list);
cardCount = countCards(shuffledDeck);
Assert.True(CompareCounts(expect, cardCount));
}
private static bool CompareCounts(Dictionary<Card, int> expect, Dictionary<Card, int> cardCount)
{
foreach (KeyValuePair<Card, int> entry in
{
Console.WriteLine("Entry:");
Console.WriteLine("card " + entry.Key.getID() + ", count " + entry.Value);
Console.WriteLine("expected:");
if (!cardCount.ContainsKey(entry.Key))
{
return false;
}
Console.WriteLine("before");
Console.WriteLine("card " + entry.Key.getID() + ", count " + cardCount[entry.Key]);
Console.WriteLine("after");
Console.WriteLine();
if (!(entry.Value == cardCount[entry.Key]))
{
return false;
}
}
return true;
}