Click here to Skip to main content
13,042,819 members (76,115 online)
Rate this:
Please Sign up or sign in to vote.

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

 <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"
        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 25-Sep-12 18:47pm
Updated 25-Sep-12 19:47pm
Mehdi Gholam 26-Sep-12 0:58am
What have you done so far?
rohith119 26-Sep-12 1:48am
I have updated my question

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
rohith119 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.
rohith119 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.
rohith119 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??
Dave Kreskowiak 26-Sep-12 23:42pm
You can "mean" to do that all you want. It's not going to happen unless you write code somewhere to build the table to comare to AND write more code to compare the fields one by one.

rohith119 27-Sep-12 0:05am
So in short I need to write more code.... :( and there is no short cut for this :(

Thanks Dave
Your explanation was good. Now i got a clear picture about the problem. Thank you for guiding me.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 26 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100