Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Hi,

I am trying to test a function which returns a datatable using visual studio 2010 testing tool. I am not able to find a correct way to do that. Can anybody help me in doing that?

I am using the below code for testing a datatable

VB
<TestMethod(), _
    HostType("ASP.NET"), _
    AspNetDevelopmentServerHost("<PathOmitted>/<SolutionName>"), _
    UrlToTest("http://localhost:2600/<SolutionName>"), _
    DeploymentItem("<Dll>")> _
   Public Sub GetDivisionCodeTest()

       Dim divDt As New DataTable
       divDt.TableName = "GetDivCode"
       divDt.Columns.Add("BU_CD")
       divDt.Columns.Add("BU_NM")
       Dim row As DataRow = divDt.NewRow
       'divDt.Rows.Add("", "")
       divDt.Rows.Add("51", "HR departments51")
       divDt.Rows.Add("25", "Service Center1")
       divDt.Rows.Add("24", "Testing")
       divDt.Rows.Add("23", "Product")
       divDt.Rows.Add("22", "Dot Net")
       divDt.Rows.Add("21", "Application")
       divDt.Rows.Add("20", "Management")
       divDt.Rows.Add("50", "Sales")
       divDt.Rows.Add("54", "HRT")
       divDt.Rows.Add("37", "Division")
       divDt.Rows.Add("75", "Section 75")
       divDt.Rows.Add("58", "DivDivision")
       divDt.Rows.Add("52", "Sales Department")
       divDt.Rows.Add("89", "skills")
       divDt.Rows.Add("87", "ski")
       divDt.Rows.Add("98", "Division Data")
       divDt.Rows.Add("56", "Training New batch 101")
       divDt.Rows.Add("90", "Department HRD")
       divDt.Rows.Add("35", "Training New batch")
       divDt.Rows.Add("45", "Center")
       divDt.Rows.Add("12", "HR departments1")
       divDt.Rows.Add("99", "software service Center")
       divDt.Rows.Add("64", "Department346")
       divDt.Rows.Add("97", "division9797")

       Dim target As IDRSMaintenance = ServiceLocator.Resolve(Of IDRSMaintenance)() ' TODO: Initialize to an appropriate value
       Dim expected As DataTable = divDt ' TODO: Initialize to an appropriate value
       Dim actual As DataTable
       actual = target.GetDivisionCode()
       Assert.AreEqual(expected, actual)
   End Sub


I am getting the following error.

Assert.AreEqual failed. Expected:<getdivcode>. Actual:<getdivcode>.


I am fetching the actual datatable from my actual code.
Posted
Updated 25-Sep-12 19:47pm
v4
Comments
Mehdi Gholam 26-Sep-12 0:58am    
What have you done so far?
Rohith Gopi 26-Sep-12 1:48am    
I have updated my question

1 solution

What's the problem?? Arrange the method call, using Fakes if you have to, call it (Act), then Assert the resulting return value for the existance of an object, it's correct type, expected data, ... It's not much different from getting back a simple value type.

There's little else anyone else can say because you haven't described what you have to do to setup the call or what you're even having a hard time with.
 
Share this answer
 
Comments
Rohith Gopi 26-Sep-12 2:52am    
I am not getting what you are trying to tell.
Dave Kreskowiak 26-Sep-12 8:14am    
That much is obvious.

You can't compare two tables the way you are. They will never be equal because they are two different objects with two different addresses in memory. You are no comparing the contents of the data tables, you are comparing the tables themselves.

Building that datatable is a waste of time. All you do is look for those values in the table that's returned from the service.
Rohith Gopi 26-Sep-12 8:28am    
Thank you Dave Kreskowiak,

Can you tell me how to compare just the values of the datatable?
Dave Kreskowiak 26-Sep-12 18:30pm    
Seriously?? You can't do:

If MyDataTable(rowNumber).Item(columnNumer) <> someValue Then...

Try reading the documentation. This is basic array handling.
Rohith Gopi 26-Sep-12 23:12pm    
But this method will be a lengthy one rite..!!?? if there are lot of columns i have to write a line for each and every column.. any other way to do this?? i mean to compare all the column values at once??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900