0

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>();
    }
0

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the help. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.