我目前使用下面的代码位的一类初始化一些单元测试:单元测试组织
[TestClass]
public class BoardTests_SquarePieceFalling
{
private Engine engine;
private BackgroundBoard backgroundBoard;
private PieceBoard pieceBoard;
private IFallingPieceFactory GetFallingPieceFactory(FallingPiece fallingPieceToReturn)
{
var factory = new Mock<IFallingPieceFactory>();
factory.Setup(f => f.Generate()).Returns(fallingPieceToReturn);
return factory.Object;
}
private void TestInitializer(Color pieceColor, Size boardSize) {
backgroundBoard = new BackgroundBoard(boardSize);
pieceBoard = new PieceBoard(boardSize);
var fallingPiece = new FallingPiece(new SquarePiece(), pieceColor, boardSize.Width);
var fallingPieceFactory = GetFallingPieceFactory(fallingPiece);
var currentFallingPiece = new CurrentFallingPiece(fallingPieceFactory);
var fallingPieceMovementEvaluator = new FallingPieceMovementEvaluator(backgroundBoard, currentFallingPiece, boardSize);
engine = new Engine(backgroundBoard, pieceBoard, currentFallingPiece, fallingPieceMovementEvaluator);
}
...Unit-Tests are below
[TestMethod]
public void When_Square_Hits_The_Ground_Another_One_Starts_Falling_From_The_Top()
{
TestInitializer(Color.Red, new Size(2, 7));
engine.Tick(10);
..Asserts..
}
...
}
现在,我想我现在有这个类太多的测试方法。我也相信他们涵盖了很多理由,所以我想将它们分成更小的测试课。
我的问题是:
目前,这是初始化测试的最好方法?我知道我可以使用[TestInitialize]注释,但是这不允许我将参数传递给我想要使用的字段的初始化,是吗?
我打算在2-3个较小的测试类中拆分当前的测试类。我目前的想法是创建一个初始化逻辑所在的基类测试类,然后让所有这些新类继承它。我看到的唯一区别是必须将
Engine
,BackgroundBoard
和PieceBoard
作为受保护字段。这是一个好主意吗?您通常如何处理非常相似的单元测试,但通常至少有一个不同的设置字段?我的意思是,在我的许多单元测试中,我有相同的
Engine
,BackgroundBoard
,PieceBoard
,FallingPiece
,CurrentFallingPiece
,FallingPieceFactory
等等,但是通常这些东西中的一个或两个对于每个测试都是不同的。我通过在每次测试中用我需要的参数定义TestInitializer()
方法来避开这个问题,但我仍然想知道是否有其他方法可以做到这一点。
感谢