12
我是單元測試我的一個函數。這裏是我的代碼:在單元測試中添加模擬數據表
public void TestLabels()
{
//Step 1: Creating a mock table with columns exactly like in the real table.
DataTable table = new DataTable();
DataRow mydatarow;
mydatarow = table.NewRow();
//Step 2: Adding the row as same as the Real Data!
mydatarow["Name"] = "Test";
mydatarow["Address"] = "00000 ST.";
mydatarow["ZipCode"] = "77665";
mydatarow["Tracking#"] = "";
table.Rows.Add(mydatarow);
foreach (DataColumn column in table.Columns)
Console.WriteLine(column.ColumnName);
//Step 3: Call method we are testing.
var updateTable = IceTechUPSClient.Instance.CreateLabels(table);
foreach (DataRow row in updateTable.Rows)
{
var trackingNumber = row["Tracking#"].ToString();
Assert.IsFalse(String.IsNullOrWhiteSpace(trackingNumber), "Expecting tracking number generated for every row!");
Assert.IsTrue(File.Exists(trackingNumber + ".gif"));
}
}
現在我收到一個錯誤:列'名稱'不屬於表。正如你所看到的,我已經在這裏指定了列名「Name」,並且還添加了特定的行。那爲什麼我得到這個錯誤?任何幫助?
謝謝!
uuummm ...我想我使用相同的代碼。 – 2010-12-14 21:00:48
這是我創建列的地方:'DataRow mydatarow; mydatarow = table.NewRow(); //第2步:添加與真實數據相同的行! mydatarow [「Name」] =「測試」; mydatarow [「Address」] =「00000 ST。」; mydatarow [「ZipCode」] =「77665」; mydatarow [「Tracking#」] =「」;' – 2010-12-14 21:02:16
這裏的列名是:姓名,地址,ZipCode和Tracking#。 – 2010-12-14 21:02:51