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

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

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

简要说明:每次用户点击“新游戏”时,新的游戏(由GameFactory)创建,棋盘需要清除/重置。在 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);
    }
}

细胞和细胞工厂

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 独立于游戏,但它有自己的 Cells 和 CellFactory。

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

这是我已经做过的测试。 (重置在 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 使用一个简单的 stub ,使用 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);
} 

单元测试 意味着您测试单个逻辑单元。因此,用假货替换所有依赖项,让您能够只测试一个逻辑单元。

关于c# - 单元测试和集成测试之间的界限在哪里(例如)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34583862/

相关文章:

c# - 如何将实体推送到 Rx Observable 上?

c# - 为什么此实体的属性缺少 SET 语句

c# - 如何遍历列表并从中创建一个 json 数组?

c# - 遍历包含对象的字典

c# - 为单元测试设置类的只读属性

python - Twisted 中的单元测试客户端-服务器交互

java - Struts2 Action - 测试与否?

c# - Silverlight : Images vs. 向量中的图标

c# - 如何以安全的方式获取弱引用的目标

.net - WPF - 数据绑定(bind)到同一控件的属性