English 中文(简体)
NUnit写测试代码
原标题:
  • 时间:2009-04-07 05:31:58
  •  标签:

下面我如何编写代码的方法,这样就可以将测试在NUnit吗?如何处理一个<代码>散列表> < /代码?

public DataSet MySampleMethod(int param1, string param2, Hashtable ht)
{
    if(ht==null)
    {
        ht = new Hashtable();
    }
    ht.Add("testKey","testData");

    DataSet ds = new DataSet();
    ds.Tables.Add();
    ds.Tables[0].Columns.Add("Column1");
    ds.Tables[0].Columns.Add("Column2");
    ds.Tables[0].Columns.Add("Column3");

    DataRow dr = ds.Tables[0].NewRow();
    dr["Column1"] = "My column 1";
    dr["Column2"] = "My column 2";
    dr["Column3"] = "My column 3";
    ds.Tables[0].Rows.Add(dr);

    DataRow dr1 = ds.Tables[0].NewRow();
    dr1["Column1"] = param1.ToString();
    dr1["Column2"] = param2;
    dr1["Column3"] = ht["testKey"].ToString();
    ds.Tables[0].Rows.Add(dr1);

    return ds;
}
最佳回答

第一个问题问的是:为什么我需要写这个方法?这年代什么给我做吗?

Give the method a more human-friendly name. From what I can see, the method takes in an integer, a string and a hashtable. The method is then expected to return a dataset containing a solitary table with 3 columns,

  • the first row contains values like {"My Column {ColumnNo}"..}
  • the second row of which contains the [ intParam.ToString(), stringParam, hashtable["testKey"] ]

Testing this method should be trivial, Test#1:

  1. Arrange : Create known inputs (an int I , string S, a hashtable with some "testData"=> Y)
  2. Act : Call the method and obtain the resulting dataset
  3. Assert : Query the dataset to see if it has the single table with 2 records. Inspect the contents of the records of the table to see if they contain the header row and the row with [I, S, Y].

Test#2: Similar to above test, except that you pass in null for the hashtable parameter.

That s all I could see based on the snippet you posted. HTH

<强> < /强>更新:无法确定你的意思在这里“处理哈希表”或“散列表的代码编写测试夹具”?哈希表是一个参数函数. .所以我认为测试会是这样(原谅坏命名和缺乏常数……t能说出他们的名字,除非我知道这个函数是用于现实生活中)

[Test]
public void Test_NeedsABetterName()
{
  int intVal = 101; string stringVal = "MyString"; string expectedHashValue = "expectedValue";
  Hashtable ht = new Hashtable();
  ht.Add("testKey", expectedHashValue);

  Dataset ds = MySampleMethod(intVal, stringVal, ht);

  Assert.AreEqual(1, ds.Tables.Count);
  Assert.AreEqual(2, ds.Tables[0].Rows.Count);
  // check header Row1.. similar to Row2 as shown below
  DataRow row2 = ds.Tables[0].Rows[1];
  Assert.AreEqual(intVal.ToString(), row2["Column1"]);
  Assert.AreEqual(stringVal,         row2["Column2"]);
  Assert.AreEqual(expectedHashValue, row2["Column3"])
}

我建议得到一本好书像务实的单元测试与NUnit c#或者从列表中< a href = " https://stackoverflow.com/questions/31837/best-books-about-tdd " > < / >速度你这里。

问题回答

暂无回答




相关问题
热门标签