jeudi 13 avril 2017

Is it the right place to use mocking in Unit test?

The question is at the bottom, a little context first:

I have two nested factories, let's say Team and Player. Let's say there's a business rule is that there must be created 5 players with number 0 and name "Bot" upon team creation (and added to this team).

I have two factories:

public class PlayerFactory : IPlayerFactory
{
    public IPlayer Create(int number, string name)
    {
        return new Player(number, name);
    }
}

public class TeamFactory : ITeamFactory
{
    private IPlayerFactory PlayerFactory { get; }

    public TeamFactory(IPlayerFactory playerFactory)
    {
        PlayerFactory = playerFactory;
    }

    public ITeam Create(int height, int width)
    {
        // ... do some operations to create players
        List<IPlayer> players = ...
        return new Team(players);
    }
}   

so far, so good. Now.. I want to create tests (ok, technically I created tests first):

[TestFixture]
public class PlayerFactoryTests
{
    [Test]
    public void CreatePlayerNumberTest()
    {
        IPlayerFactory playerFactory = new PlayerFactory();
        IPlayer player = playerFactory.Create(2, "");
        Assert.AreEqual(2, player.Number);
    }

    [Test]
    public void CreatePlayerNameTest()
    {
        IPlayerFactory playerFactory = new PlayerFactory();
        IPlayer player = playerFactory.Create(2, "John");
        Assert.AreEqual("John", player.Name);
    }
}

[TestFixture]
public class TeamFactoryTests
{
    [Test]
    public void CreateTeamTest()
    {
        IPlayerFactory playerFactory = new PlayerFactory();
        ITeamFactory teamFactory = new TeamFactory(playerFactory);

        ITeam team = teamFactory.Create(); 
    }
}

The question is:

Should I mock IPlayerFactory in CreateTeamTest test? If yes, How should I do that?

Aucun commentaire:

Enregistrer un commentaire