How can i write Code for the below method so that it can be tested in NUnit.? How to handle Hash table.?
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;
}
-
First question to ask is: Why do I need to write this method? What's it doing for me?
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:
- Arrange : Create known inputs (an int I , string S, a hashtable with some "testData"=> Y)
- Act : Call the method and obtain the resulting dataset
- 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
Update: Not sure what you mean here by "handle hashtable" or "write test fixture code for hashtable" ? The hashtable is just a parameter to your function.. so I reckon the test would look something like this (Forgive the bad naming and lack of constants... can't name them unless I know what this function is used for in real life)
[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"]) }I'd recommend getting a good book like Pragmatic Unit Testing in C# with NUnit or one from the list here to speed you up here.
Arunachalam : I just need to know how we could pass values for the hashtable. and i also need to know how will you write a test fixture code for HashTable so that it could be tested in Nunit.?Arunachalam : the above post is sent by my friend so i replied on behalf of himGishu : What do you mean by test fixture code for Hashtable.. I'm hoping you're not trying to test hashtable functionality.. could you explain your problem a bit more... I sense a disconnect.
0 comments:
Post a Comment