views:

60

answers:

1

I'm new to visual studio tests, I created a new test method to test a method which returns a datatable based on the tablename passed, but I'm confused as to how to test such a method, since the results are variable based on what's in the backend store at any point in time

[TestMethod()]
        public void GetTableDataTest()
        {
            string tableName = "SomeTable"; // TODO: Initialize to an appropriate value
            DataTable expected = null; // TODO: Initialize to an appropriate value
            DataTable actual;
            actual = LogManager.GetTableData(tableName);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

Also can someone please point me to a some good blog resources?

+1  A: 

A proper unit test does not access a live database. A unit test should operate against mock or fake objects. That is, you'll have to make sure that the LogManager.GetTableData() can be designed to return fake data when being run from the unit test.

For example, the fake data could be a list of static log entries called "Log Entry 1" through "Log Entry 5". Then the unit test verifies that the method in fact had 5 log entries, and each had the right name.

The real question here boils down to "What exactly is it that you are trying to test?" Define the scenario and then we can help you write the test.

Eilon
Thanks for that response, clears some cloudiness I had in my mind. when you say "LogManager.GetTableData() can be designed to return fake data when being run from the unit test."do you modify the actual method for this?Can you please provide an example for this?
Nevin Mathai
It really depends on what exactly you're trying to test. What is behavior does the GetTableData() method have that you are trying to verify? Does it do some processing? Filtering?
Eilon
Yes, I actually removed the other parameters which were basically filters to how the database query to that table gets built. The method constructs the database query and then runs the query and returns a datatable back
Nevin Mathai
So what behavior is it that you want to verify? That it constructed the right query? Based on what data does it construct this query? And where does this query then go? (That is, where is the query itself used?)
Eilon