为什么传递变量失败,错误值类型'string'无法转换为'string()'?

问题描述 投票:0回答:1

我正在尝试使用VB为某些C#代码创建自动测试。示例:对于表Orchestra,我有1条记录,记录了组织1123,其中用户xxx的值为externalId = null,名称为Charm White。因此,我的测试将是检查期望的externalId是否为null且该用户的名称是否为白色。

但是,在VB代码中声明值类型为string [] externalIds的变量时,它给出了错误:

'String'类型的值不能转换为'string()'

这是要测试的C#代码:

  public string[] GetNamesFromExternalIds(string organisationId, string[] externalIds)
    {
        string externalIdsString = null;
        foreach (string externalId in externalIds)
        {
            if (externalIdsString != null)
            {
                externalIdsString += ",";
            }
            externalIdsString += "'" + externalId + "'";
        }

        DbParameterCollection parameters = new DbParameterCollection();
        parameters.Add(new DbParameter("@OrganisationId",SqlDbType.NVarChar, 
        organisationId));

        string sql = string.Format(@"SELECT ExternalId, Name FROM Orchestra 
        WHERE ExternalId IN ({0}) AND OrganisationId = @OrganisationId",
        externalIdsString);

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

        List<string> names = new List<string>();
        foreach (string externalId in externalIds)
        {
            foreach (DataRow dataRow in list.Table.Rows)
            {
                if (dataRow["ExternalId"].ToString() == externalId)
                {
                    names.Add(dataRow["Name"].ToString());
                    break;
                }
            }
            names.Add(null);
        }

        return names.ToArray();
    }

这是我要尝试的VB:

<TestClass()> Public Class UT_GetNamesFromExternalIds
    <TestMethod()> Public Sub GetNamesFromExternalIds()

        Dim organisationId As String = "1123"

        Dim externalIds() As String ={0}
        Dim user as string = "XXX"
        Dim names As String = String.Empty

        Dim ExpNames As String = "Charm White"
        Dim ExpexternalIds As String = Nothing

        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)

        externalIds = AD.GetNamesFromExternalIds(organisationId, 
         externalIds)

        Assert.AreEqual(1, externalIds.Count)
        Assert.AreEqual(ExpexternalIds, externalIds(0))


        Assert.AreEqual(ExpNames, names)

        Console.WriteLine(ExpNames)
        Console.WriteLine(names)

        Console.WriteLine(ExpexternalIds)
        Console.WriteLine(externalIds)
    End Sub

End Class
c# vb.net mstest
1个回答
0
投票

假设您尝试将0001214传递到被测函数中,并期望将Charm White作为结果,那么您需要使用0001214值创建一个字符串数组-将其传递给函数并测试结果:

Dim externalIds As String() = {"0001214"}
Dim organisationId As String = "1123"
Dim names = AD.GetNamesFromExternalIds(organisationId, externalIds)

Dim ExpNames As String = "Charm White"

Assert.AreEqual(1, names.Count)
Assert.AreEqual(ExpNames, names(0))

也删除行:

Dim names As String = String.Empty

因为从被测函数返回的值是字符串数组而不是字符串。我还移动了organisationId的声明,使其接近使用它的代码。

© www.soinside.com 2019 - 2024. All rights reserved.