Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

C# Xunit testing a list that must contain 4 of each value

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your assertion can group the cards by value. You'd then have 13 total groups (13 cards of different value in each suit). Alternatively, you could group by suit, which would mean 4 groups with 13 members... though that doesn't prove they're unique).

For the former,

Assert(cardDeck.GroupBy(x => x.value).Count() == 13).

Of course change that assertion to the assertion language of the testing package you're using (not sure about XUnit), e.g. if using Fluent Assertions, that would be

cardDeck.GroupBy(x => x.value).Should().HaveCount(13)

Also a few notes:

  • Your current algorithm is not correct. It would fail this assertion. I leave that to you.
  • This does not prove that the values for suit are correct, for example, especially because you're using int for suit despite declaring it as an enum

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...