c#-4.0 – 在单元测试中添加模拟数据
发布时间:2020-12-15 07:49:06 所属栏目:百科 来源:网络整理
导读:我是单位测试我的一个功能.这是我的代码: 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: Addi
我是单位测试我的一个功能.这是我的代码:
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”,并添加了该行.那么为什么我得到这个错误?任何帮助? 谢谢! 解决方法
您尚未设置列(除非您错过了示例中的某些代码).
您需要创建具有所需名称的列,然后才能访问它们: var columnSpec = new DataColumn { DataType = typeof(string),ColumnName = "Name" }; this.table.Columns.Add(columnSpec); 当您将AutoGenerateColumns设置为true(默认值)时,如果您从数据库中读取数据,则无需明确地将其视为幕后完成. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |