vendredi 19 mars 2021

How to properly model and test a method that is dependent from another?

I'm developing an example of card game. And while developing that, i couldn't find a way to properly create a unit test for the method DistributeCards(). And that's because, the DistributeCards() method is dependent of the StartNewRound() method to run. And inside the StartNewRound() method, there's a call to the DistributeCards() method.

GameManager Class:

public class GameManager
{

    private Game _Game { get; set; }

    public GameManager(Game game)
    {
        _Game = game;
    }

    public void AddPlayer(Player player)
    {
        _Game.PlacePlayer(player);
    }

    public void StartNewRound()
    {
        // Shuffle Cards
        _Game.ShuffleCards();
        // Update current playing player
        _Game.PlayingPlayer = _Game.GetPlayerAtChair(_Game.GetNextOccupiedChairIndex(0));

        DistributeCards();
    }

    public void DistributeCards()
    {
        //Make Card Distribution
    }
}

So my test was going to be like the following:

[TestFixture()]
public class GameManagerTests
{
    private GameManager _manager;

    [SetUp]
    public void SetUp()
    {
        _manager = new GameManager(new Game(6));
    }

    [Test()]
    public void DistributeCardsTest()
    {
        _manager.AddPlayer(new Player("1", "test", 10));
        _manager.AddPlayer(new Player("2", "test", 10));
        _manager.AddPlayer(new Player("3", "test", 10));
        _manager.AddPlayer(new Player("4", "test", 10));

        _manager.StartNewRound();
        
    }
}

We can see in the DistributeCardsTest() test method that at the point that i call StartNewRound() the DistributeCards() method has already run. And if i would call it again, i would be running it twice, and in my head that a little bit strange.

So i don't know if i can model the method to be more testable somehow or if i should change something in the test itself like creating a mock or something like that.

Obs.: I'm trying to use DDD modeling.

Aucun commentaire:

Enregistrer un commentaire