views:

61

answers:

2

I am currently writing unit tests for an application near completion (before management looks at it and turns it inside out). I am testing the business layer and a method is set to return a datatable of all "GroupID"s, which are simply an integer to represent the group that a user belongs to.

I'm assuming the best way to assert on this unit test would be to assert that the correct groupIDs are being returned from a controlled test database. However, I'm not sure how to assert on a datatable. Is the only (and best) possible way to accomplish this to simply loop through the datatable until it's empty and add each value to an array of integers? How would one go about asserting on this test?

A: 

It depends how the method in the business layer gets it's data. If it's direct from the database, then you don't have much choice than to use a test database. This isn't in the spirit of unit tests though, really.

Ideally the business layer method would get it's data from a data provider that you can swap out at runtime using something like IoC (e.g. Castle Windsor) or a mocking framework like Rhino Mocks.

Either way, you essentially need to setup a test, and compare the return values of methods with expected values. Whether you use a database or mock object doesn't affect this approach, so checking the contents and order of an array against a hard-coded known-value is perfectly acceptable.

Neil Barnwell
It recieves the data from a datalayer, which in most cases just forwards the data along.
Chris
A: 

best way to accomplish using collection...

List<string> items = new List<string>(dtbl.Rows.Count);

    for (int RowCount = 0; RowCount < dtbl.Rows.Count; RowCount++)
    {
        items.Add(dtbl.Rows[RowCount]["ID"].ToString());
    }

    items.ToArray();
Muhammad Akhtar