So I'm practicing unit testing (Xunit) and C#. I'm making a poker game console application. I have a card deck that is a list of 52 cards. The list is made of Card class and each card has an int value that represents what card it is eg value 1 is ace and value 13 is king.
How could I make an unit test that checks that the list of 52 cards has 4 of each value? I'm just doing it for practice and can't think of an unit test that's not too complicated.
Here's the card class very simple:
public class Card
{
public enum Suit
{
HEART = 1,
SPADE,
CLUB,
DIAMOND
}
public int value { get; set; }
public int suit { get; set; }
public char letter { get; set; }
}
CreateDeck() method creates and returns the card deck:
public class CardDeck
{
public static List<Card> CreateDeck()
{
List<Card> cardDeck = new List<Card>();
int maxCards = 52;
int currentCardValue = 1;
int currentSuitValue = 1;
for (int i = 0; i < maxCards; i++)
{
cardDeck.Add(new Card { value = currentCardValue, suit = currentSuitValue, letter = Card.ReturnLetter(currentCardValue) });
currentSuitValue++;
if (currentSuitValue > 4) { currentSuitValue = 1; }
//After 4 cards of the same value we raise the cards value
if(i + 1 % 4 == 0)
{
currentCardValue++;
}
}
return cardDeck;
}
}
So I want to make a Xunit test:
[Fact]
public void CardDeck_ShouldHave4CardsOfEachValue()
{
List<Card> cardDeck = CardDeck.CreateDeck();
??????
}
question from:
https://stackoverflow.com/questions/65943988/c-sharp-xunit-testing-a-list-that-must-contain-4-of-each-value 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…