Модульный тест VS 2010 и тип без знака - PullRequest
1 голос
/ 09 июля 2009

Я преобразую свои модульные тесты из NUnit в среду модульных тестов VisualStudio 2010. Следующий метод ByteToUShortTest() завершается с сообщением:

Assert.AreEqual failed. Expected:<System.UInt16[]>. Actual:<System.UInt16[]>.

[TestMethod, CLSCompliant(false)]
public void ByteToUShortTest()
{
    var array = new byte[2];
    Assert.AreEqual(ByteToUShort(array), new ushort[1]);
}

Код, вызываемый тестом:

[CLSCompliant(false)]
public static ushort[] ByteToUShort(byte[] array)
{
    return ByteToUShort(array, 0, array.Length, EndianType.LittleEndian);
}

public enum EndianType
{
    LittleEndian,
    BigEndian
}

[CLSCompliant(false)]
public static ushort[] ByteToUShort(byte[] array, int offset, int length, EndianType endianType)
{
    // argument validation
    if ((length + offset) > array.Length)
        throw new ArgumentException("The length and offset provided extend past the end of the array.");
    if ((length % 2) != 0)
        throw new ArgumentException("The number of bytes to convert must be a multiple of 2.", "length");

    var temp = new ushort[length / 2];

    for (int i = 0, j = offset; i < temp.Length; i++)
    {
        if (endianType == EndianType.LittleEndian)
        {
            temp[i] = (ushort)(((uint)array[j++] & 0xFF) | (((uint)array[j++] & 0xFF) << 8));
        }
        else
        {
            temp[i] = (ushort)(((uint)array[j++] & 0xFF) << 8 | ((uint)array[j++] & 0xFF));
        }
    }

    return temp;
}

Этот тест успешно выполнялся с NUnit. Есть идеи, почему типы должны быть разными?

Решение

Для одиночных и многомерных массивов, а также для любых ICollection среда модульного тестирования VisualStudio 2010 предоставляет класс CollectionAssert .

[TestMethod, CLSCompliant(false)]
public void ByteToUShortTest()
{
    var array = new byte[2];
    CollectionAssert.AreEqual(ByteToUShort(array), new ushort[1]);
}

1 Ответ

4 голосов
/ 09 июля 2009

Это не типы, а экземпляры.Вы сравниваете два разных массива.

...