单元测试和集成测试之间的界限在哪里(例如)?

Where is the line between unit and integration test (on example)?

我无法理解这两种测试之间的界线在哪里(或应该在哪里)。所以,我有一个虚拟的例子:一个简单的井字游戏。游戏有自己的棋盘 (3x3),有 9 个单元格。依赖关系很明显:Game <- Board <- Cell.

快速描述:每次用户点击"New Game",新的Game被创建(由GameFactory)并且Board 需要 cleared/reset。在 GameFactory 的整个生命周期中,只有一个 Board 对象。重置 Board 意味着创建全新的 Cell 对象。这是代码:

GameFactory:(如你所见,我创建了一次 Board,所以我需要在 Game 构造函数中重置它):

public class GameFactory : IGameFactory
{
    private readonly IBoard board;
    public GameFactory(IBoard board)
    {
        this.board = board;
    }
    public IGame Create()
    {
        return new Game(board);
    }
}

Cell 和 CellFactory

public interface ICell
{
    int PlayerId { get; set; }
    int Row { get; set; }
    int Column { get; set; }
}

public interface ICellFactory
{
    ICell Create(int row, int column);
}

public class CellFactory : ICellFactory
{
    public ICell Create(int row, int column)
    {
        return new Cell(row, column);
    }
}

最后,董事会

public interface IBoard
{
    int Width { get; }
    int Height { get; }

    void Reset();

    // Rest is not important for that question
    // ... 
}

public class Board : IBoard
{
    private ICell[,] cells;
    private readonly ICellFactory cellFactory;

    public int Width { get; }
    public int Height { get; }

    public void Reset()
    {
        cells = new ICell[Height, Width];
        for (int i = 0; i < cells.GetLength(0); i++)
        {
            for (int j = 0; j < cells.GetLength(1); j++)
            {
                cells[i, j] = cellFactory.Create(i, j);
            }
        }
    }

    // Rest is not important for that question
    // ...
}

问题:如何测试 Board 对象中的 Reset 方法? Board 独立于 Game,但它有自己的 Cells 和 CellFactory。

几个其他相关问题: - 是否可以创建 Board 单元测试?我可以说,如果一个对象依赖于其他对象(即使它们是接口)那么它已经必须是集成测试,而不是单元测试?

这是我已经做过的测试。 (在 Board 构造函数中调用重置):

    [Test]
    public void BoardCreationTest()
    {
        var cellFactory = new CellFactory();
        IBoard board = new Board(3, 3, cellFactory);

        for (int i = 0; i < board.Height; i++)
        {
            for (int j = 0; j < board.Width; j++)
            {
                // Check if board.cells[i,j].PlayerId is zero (it has to be zero, player zero is empty cell)
                // Another thing is that cells are private, cause project doesn't need it public
                // Should I make cells public just for tests?

                // Right now I'm checking it IsMoveValid(column, row, playerId)
                // It has to be true, when player 1 wants move in certain cell (it has to be zero, player zero is empty cell)
                Assert.IsTrue(board.IsMoveValid(i, j, 1));
            }
        }
    } 

编辑:电路板构造函数:

    public Board(int width, int height, ICellFactory cellFactory)
    {
        Width = width;
        Height = height;
        this.cellFactory = cellFactory;

        Reset();
    }

EDIT2:我现在的整个测试。它通过了:

[TestFixture]
class BoardTests
{
    private IBoard board;

    [SetUp]
    public void RunBeforeAnyTests()
    {
        var cellFact = Substitute.For<ICellFactory>();
        cellFact.Create(Arg.Any<int>(), Arg.Any<int>())
            .Returns(x => new Cell((int)x[0], (int)x[1]));
        board = new Board(3, 3, cellFact);
    }

    [Test]
    public void BoardCreationTest()
    {
        board.Reset();

        for (int i = 0; i < board.Height; i++)
        {
            for (int j = 0; j < board.Width; j++)
            {
                Assert.IsTrue(board.IsMoveValid(i, j, 1));
            }
        }
    }
}

您可以为 CellFactoryCell 使用简单的存根,例如使用 NSubstitute。 在这种情况下,您将仅测试 Reset 方法逻辑

[Test]
public void BoardCreationTest()
{
    var board = CreateBoard();

    //Calling method explicitly makes test more readable
    board.Reset();

    for (int i = 0; i < board.Height; i++)
    {
        for (int j = 0; j < board.Width; j++)
        {
           Assert.IsTrue(board.IsMoveValid(i, j, 1));
        }
    }
}

private IBoard CreateBoard()
{
    var cellFactory = Substitute.For<ICellFactory>();
    //You may replace Cell with another mock or smth else. Depends on your assert section.
    cellFactory.Create(Arg.Any<int>(), Arg.Any<int>())
       .Returns(x => new Cell((int)x[0], (int)x[1]));        
    IBoard board = new Board(3, 3, cellFactory);
} 

Unit test 表示您测试单个逻辑单元。因此,用假货替换所有依赖项,让您能够只测试一个逻辑单元。