我正在尋找使用NDbUnit來幫助進行應用程序的單元測試。正如問題標題所述,我想知道是否可以將NDbUnit測試數據保存在單獨的XML文件中。我已經注意到,我的單個測試數據XML文件相當大,並且可能會在添加更多實體時變得無法管理。是否可以將NDbUnit測試數據保存在單獨的XML文件中?
現在,閱讀this問題,看起來好像不可能,但我只想確定一下。
如果有幫助,這是說明問題的示例代碼。這個想法是,程序與供應商相關聯。我已經建立了包含3個供應商的測試數據,其中第二個有3個程序。 TestData.xml包含所有供應商和程序的所有測試數據。當我使用它時,單元測試按預期通過。如果我試圖通過多次調用db.PerformDbOperation(DbOperationFlag.CleanInsertIdentity);
來分別讀取單個XML文件,那麼看起來好像第二個調用會覆蓋第一個調用的第一個調用。
private const string xmlSchema = @"..\..\schema.xsd";
// All of the test data in one file.
private const string xmlData = @"..\..\XML Data\TestData.xml";
// Individual test data files.
private const string vendorData = @"..\..\XML Data\Vendor_TestData.xml";
private const string programData = @"..\..\XML Data\Program_TestData.xml";
public void WorkingExampleTest()
{
INDbUnitTest db = new SqlDbUnitTest(connectionString);
db.ReadXmlSchema(xmlSchema);
db.ReadXml(xmlData);
db.PerformDbOperation(DbOperationFlag.CleanInsertIdentity);
VendorCollection vendors = VendorController.List();
Assert.IsNotNull(vendors);
ProgramCollection collection = VendorController.GetPrograms(vendors[1].VendorID);
Assert.IsNotNull(collection);
Assert.IsTrue(collection.Count == 3);
}
public void NotWorkingExampleTest()
{
INDbUnitTest db = new SqlDbUnitTest(connectionString);
db.ReadXmlSchema(xmlSchema);
db.ReadXml(vendorData);
db.PerformDbOperation(DbOperationFlag.CleanInsertIdentity);
db.ReadXml(programData);
db.PerformDbOperation(DbOperationFlag.CleanInsertIdentity);
VendorCollection vendors = VendorController.List();
Assert.IsNotNull(vendors);
// This line throws an ArgumentOutOfRangeException because there are no vendors in the collection.
ProgramCollection collection = VendorController.GetPrograms(vendors[1].VendorID);
Assert.IsNotNull(collection);
Assert.IsTrue(collection.Count == 3);
}
這並不工作:
這是直截了當的。謝謝sbohlen,這似乎正是我想要的。 – Malice 2011-05-23 07:34:38