我有一个Factory
类,它使用一个Generator
类(其中会有几个,每个都有不同的算法)来生成对象数组。我正在尝试使用 TDD 方法对此进行编程,但在决定测试时遇到了很大困难。我才刚刚开始使用 TDD,所以我想我的测试实际上是丑陋的可憎之物。
这些是我对以下内容的测试Factory
:
[TestFixture]
public class MapRegionFactoryTests
{
[Test,
Description("Tests if a MapRegion is successfully created.")]
public void TestBasicRegionCreation()
{
var mapRegionFactory = new MapRegionFactory();
MapRegion mapRegion = mapRegionFactory.GenerateMapRegion();
Assert.IsTrue(mapRegion != null);
}
[Test,
Description("Tests if a MapRegionFactory's RegionSizeX, RegionSizeY and RegionSizeZ are properly initialized" +
"to their default values.")]
public void TestDefaultPropertyInitialization()
{
var mapRegionFactory = new MapRegionFactory();
Assert.Greater(mapRegionFactory.RegionSizeX, 0);
Assert.Greater(mapRegionFactory.RegionSizeY, 0);
Assert.Greater(mapRegionFactory.RegionSizeZ, 0);
}
[Test,
Description("Tests if properties can only be assigned numbers above zero.")]
public void TestGreaterThanZeroPropetyAssignment()
{
var mapRegionFactory = new MapRegionFactory
{
RegionSizeX = -8,
RegionSizeY = -3,
RegionSizeZ = 0
};
Assert.Greater(mapRegionFactory.RegionSizeX, 0);
Assert.Greater(mapRegionFactory.RegionSizeY, 0);
Assert.Greater(mapRegionFactory.RegionSizeZ, 0);
}
[Test,
Description("Tests if the default RegionFactory can deliver a region filled with tiles.")]
public void TestRegionCount()
{
// Test the count of objects again?
}
}
和一个单一的测试Generator
:
[TestFixture]
public class MapRegionGeneratorTests
{
[Test,
Description("Tests that a region of a certain size is properly filled with Block objects.")]
public void TestDefaultRegionSize()
{
const int regionSize = 16;
var mapRegionGenerator = new MapRegionGenerator();
var regionData = mapRegionGenerator.GenerateRegion(regionSize);
foreach (Block element in regionData)
{
Assert.IsNotNull(element);
}
}
}
到目前为止,我是否正确地接近这个?我知道这MapRegionFactory
将IMapRegionGenerator
通过 Castle.Windsor 进行注入,但是我如何模拟像生成器这样复杂的东西呢?我无法复制整个生成代码,不是吗?