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

时间:2016-01-04 02:18:07

标签: c# .net unit-testing testing integration-testing

我在理解这两种测试之间的界限(或应该是)之间存在问题。所以,我有一个虚拟的例子:一个简单的tic-tac-toe游戏。游戏有自己的板(3x3),有9个单元。依赖性是显而易见的:游戏< - Board< - Cell

简要说明:每次用户点击"新游戏" 时,都会创建新的游戏(由GameFactory创建)和 Board 需要清除/重置。在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 and 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构造函数中调用Reset):

    [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));
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用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表示您测试单个逻辑单元。因此,用假货替换所有依赖项,使您能够仅测试一个逻辑单元。