Search code examples
vb.netmstest

Value of type String() cannot be converted into ArrayList


I'm trying to automate test some codes and I can't seem to get this working on ListObject.

If I run the test it fails with an error:

Value of type String() cannot be converted into ArrayList.

Here's what I'm trying:

C# CODE:

public string[] GetUserIdsFromPassId(string organisationId, string @PassId)
    {
        DbParameterCollection parameters = new DbParameterCollection();
        parameters.Add(new DbParameter("@OrganisationId", SqlDbType.NVarChar, organisationId));
        parameters.Add(new DbParameter("@PassId", SqlDbType.NVarChar, @PassId));

        string sql = "SELECT UserId FROM Orchestra WHERE OrganisationId=@OrganisationId AND PassId=@PassId";

        ListObject list = new ListObject(_Accessor);
        list.Read(sql, parameters);

        List<string> userIds = new List<string>();
        foreach (DataRow dataRow in list.Table.Rows)
            userIds.Add(dataRow["UserId"].ToString());

        return userIds.ToArray();
    }

AUTOMATE TESTING CODE:

<TestClass()> Public Class Check_UserIdsFromPassId
    <TestMethod()> Public Sub GetUserIdsFromPassId()

        Dim organisationId As String = "1123"
        Dim PassId As String = "8110004"

        Dim UserId As string = String.Empty

        Dim ExpUserId As String = "00044"

        Dim DataServer As New DataServer()
        Dim Accessor = DataServer.GetAccessor()
        Dim _StandardHeader = New StandardHeader
        Dim _AuditProvider = New Audit.AuditProvider(_StandardHeader)
        Dim AD As New Ceridian.Administration.Authentication.AuthenticationData(Accessor, _AuditProvider)
        UserId = AD.GetUserIdsFromPassId(organisationId, PassId)
        Assert.AreEqual(ExpUserId, UserId)
        Console.WriteLine(ExpUserId)
        Console.WriteLine(UserId)

    End Sub
End Class

Solution

  • If you are expecting the function under test to return a string array with a single UserId (ExpUserId) you could just test the length of the array and the first value.

    For instance, remove this line:

    Dim UserId As string = String.Empty
    

    Change the line that exercises the function under test to:

    Dim UserId = AD.GetUserIdsFromPassId(organisationId, PassId)
    

    and then confirm the returned array has a single correct value:

    Assert.AreEqual(1, UserId.Count)
    Assert.AreEqual(ExpUserId, UserId(0))