我创建了一个流利的构建器样式模式来帮助加载我的测试数据。某些方法的顺序很重要,并且想知道管理正确顺序的首选方法是什么。
我目前有以下内容:
using NUnit.Framework;
[TestFixture]
public class DataBuilderTests
{
[Test]
public void Can_NAME()
{
new DataLoader()
.Start() // must be called first
.Setup() // then called next
.LoadEmployees() // optional order not NB
.LoadProducts() // optional order not NB
.StartCleanup() // begin cleanup
.CleanupEmployees() // optional order not NB
.CleanupProducts() // optional order not NB
.End();
}
}
public class DataLoader
{
public DataBuilderSetup Start()
{
return new DataBuilderSetup(this);
}
}
public class DataBuilderSetup
{
private readonly DataLoader _dataLoader;
public DataBuilderSetup(DataLoader dataLoader)
{
_dataLoader = dataLoader;
}
public DataBuilderOptions Setup()
{
// do setup
return new DataBuilderOptions(_dataLoader);
}
}
public class DataBuilderOptions
{
private readonly DataLoader _dataLoader;
public DataBuilderOptions(DataLoader dataLoader)
{
_dataLoader = dataLoader;
}
public DataBuilderOptions LoadEmployees()
{
// load
return this;
}
public DataBuilderOptions LoadProducts()
{
// load
return this;
}
public DataBuilderCleanupOptions StartCleanup()
{
return new DataBuilderCleanupOptions(_dataLoader);
}
}
public class DataBuilderCleanupOptions
{
private readonly DataLoader _dataLoader;
public DataBuilderCleanupOptions(DataLoader dataLoader)
{
_dataLoader = dataLoader;
}
public DataBuilderCleanupOptions CleanupEmployees()
{
// cleanup
return this;
}
public DataBuilderCleanupOptions CleanupProducts()
{
// cleanup
return this;
}
public DataLoader End()
{
return _dataLoader;
}
}