Ray
Ray

Reputation: 480

C# TDD Testing an array[int] not working as expected

I'm trying to create a test for a method that is passed an int[] array of AllNumbers and returns an int[] array of only EvenNumbers. Although debugging the test shows me that the Expected and Actual are the same the test still fails. I'm guessing it's a .Equals versus == problem?

Error:

Failed  TestEvenNumbers CalculatorEngineTests   Assert.AreEqual failed. Expected:<System.Int32[]>. Actual:<System.Int32[]>.     

This is my test:

[TestMethod]
    public void TestEvenNumbers()
    {
        Calculator target = new Calculator();
        int[] test = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        int[] expected = { 2, 4, 6, 8 };
        int[] actual = target.GetEvenNumbers(test);

        //This passes
        Assert.AreEqual(expected[1], actual[1]);
        //This fails
        Assert.AreEqual(expected, actual);
    }

This is the method I want to test:

public int[] GetEvenNumbers(int[] arr)
    {
        var evenNums =
            from num in arr
            where num % 2 == 0
            select num;


        return evenNums.ToArray<int>();
    }

Upvotes: 0

Views: 354

Answers (1)

TVOHM
TVOHM

Reputation: 2742

Try:

CollectionAssert.AreEqual(expected, actual);

To assert that the collections contain the same things, as I believe the Assert.AreEqual will simply compare the references.

Upvotes: 3

Related Questions