views:

271

answers:

2
   public DataSet ExampleMethod(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");
    return ds ;


    }

this is just a example method now since i have a string ,a int and a hash table type as a input ,i can pass random values for int and string but what type of value will i pass for hash table type in order to unit test this method . and i also want a sample Nunit Fixture code for this method so that i can test it in Nunit framework .since my method returns a dataset how do i write a text fixture for it because i use AREequals.(5,add(2,3)) if it returns a int .so what to do for a method when it returns a Dataset

A: 

You can create a HashTable in the TestMethod or the TestFixture with dummy data and pass that object. Am I understanding you right?

In the [SetUp] method you populate your HashTable with dummy data.

Hashtable ht = new Hashtable();

[SetUp]
public void SetUp()
{
   ht.Add( "key1", "value1" );
   ht.Add( "key2", "value2" );
   ht.Add( "key3", "value3" );
}

I can see you real question is about the Assert of the returned DataSet. You can still use Assert.AreEqual, but instead test that the DataSet contain the cell data you'd expect given the HashTable.

Assert.AreEqual( ht["key1"], ds.Tables[0].Rows[0][0].ToString() );
Assert.AreEqual( ht["key1"], ds.Tables[0].Rows[0][1].ToString() );

You could use a loop for this.

Your TestMethod could look like this.

[Test]
public void Should_Do_Stuff()
{
    MyClass myObject = new MyClass();

    DataSet ds = myObject.ExampleMethod( 1, "string", ht );

    Assert.AreEqual( ds.Tables[0].Rows.Count, ht.Count );
    Assert.AreEqual( ht["key1"], ds.Tables[0].Rows[0][0].ToString() );
    Assert.AreEqual( ht["key1"], ds.Tables[0].Rows[0][1].ToString() );
}

Hope that answered you question.

Arkain
can u be more elaborate . since hash table is collections will it be proper way of unit testing it
Arunachalam
This method is returning a dataset so how should i compare the expected result and the result for example if the return type is int AREequals.(5,add(2,3)) i can verify like this
Arunachalam
Does this answer your question?
Arkain
ya but i am actually generating these code programmatic so is there a generic way to do it
Arunachalam
You have to be more specific, how and what code do you generate?
Arkain
have seen Nunit partner i am actually creating a similar tool but i want to support hah table and dataset and also collections which they have not done
Arunachalam
Can you rephrase? I have no idea what you are actually asking.
Arkain
i am developing tool similar to Nunit partner and i also want to support system.collections . so how can i create this piece of code dynamically
Arunachalam
A: 

Pass on null or create new hashtable with static data and pass it to this function. For checking dataset you can assert on returned dataset count, is null or not and also values if you want.

Brijesh Mishra