0

I'm trying to do an xUnit test on a function and it is failing. Went with the debuger and all the values are as expected. the expected result is equal with the actual result. So for the next step, trying to figure out why it is failing, I assigned the same values for the input and the expected result (without using the function I wanted to test) to make sure both are equal then used the function Asser.Equal(expectedResult, input) but again I ended up with an error (the actual and expected results are not the same) even though I made them exactly the same. The tests I'm trying to make is on structures. The next step , after instead of assigning the values for the expectedResult, I just made expectedResult = input and used Assert.Equal(expectedResult, input) and now the tests were successful. My question would be, why doesn't it work when I assign the values separately to the expectedResult and why it does work when I make the expectedResult = with the input. Is this because of the structures from the code?

    [Fact]
    // this example doesn't work
    public void GenerateGeneralRanking_OneSerriesAsInput_ShouldReturnSortedRanking()
    {
        Contest input = new Contest();
        input.GeneralRanking.Contestants = new Contestant[3];

        Contestant input1 = new Contestant ( "Ion", "Romania", 9.500 );
        Contestant input2 = new Contestant("Alex", "Romania", 9.300);
        Contestant input3 = new Contestant("Dan", "Romania", 9.200);

        input.GeneralRanking.Contestants[0] = input1;
        input.GeneralRanking.Contestants[1] = input2;
        input.GeneralRanking.Contestants[2] = input3;

        Contest expectedRanking = new Contest();
        expectedRanking.GeneralRanking.Contestants = new Contestant[3];

        expectedRanking.GeneralRanking.Contestants[0] = input1;
        expectedRanking.GeneralRanking.Contestants[1] = input2;
        expectedRanking.GeneralRanking.Contestants[2] = input3;

        Assert.Equal(input, expectedRanking);

    }

And If I make it like this it works ->

[Fact]
    public void GenerateGeneralRanking_OneSerriesAsInput_ShouldReturnSortedRanking()
    {
        Contest input = new Contest();
        input.GeneralRanking.Contestants = new Contestant[3];

        Contestant input1 = new Contestant ( "Ion", "Romania", 9.500 );
        Contestant input2 = new Contestant("Alex", "Romania", 9.300);
        Contestant input3 = new Contestant("Dan", "Romania", 9.200);

        input.GeneralRanking.Contestants[0] = input1;
        input.GeneralRanking.Contestants[1] = input2;
        input.GeneralRanking.Contestants[2] = input3;

        Contest expectedRanking = new Contest();
        expectedRanking.GeneralRanking.Contestants = new Contestant[3];

        // This would be the changed part
        expectedRanking = input;

        Assert.Equal(input, expectedRanking);

    }

The function I'm trying to test makes the same thing as the first example of the code, so I typed the values for the sake of the example. Sorry if I missed some info and thank you in advance

  • 1
    https://stackoverflow.com/questions/23530982/c-sharp-asserting-two-objects-are-equal-in-unit-tests This should give you an idea. Essentially the objects are equated by reference and they will never be equal since both point to a different memory location even though the contents inside are same. – Sharath N S Feb 26 '22 at 14:04
  • This makes sense, thank you. Looking over the post you suggested. – Bogdan Muscari Feb 26 '22 at 14:15
  • Assert.Equal(input.GeneralRanking.Contestants, expectedRanking.GeneralRanking.Contestants); this is the modified Assert.Equal which worked. I just had to compare the values from them. – Bogdan Muscari Feb 26 '22 at 15:19

2 Answers2

1

I would guess that Contest does not implement IEquatable<Contest> or otherwise override Equals(object). Therefore Assert.Equal will only work when the objects are reference equal. If you don't want to do this, you can create your own IEqualityComparer<Contest> and use the overload Assert.Equal(expectedRanking, input, equalityComparer). Best of luck

Andrew McClement
  • 1,171
  • 5
  • 14
  • Thank you, I understand where the problem is now. I was confused about the fact that the results were the same but somehow the error kept coming back. Didn't thought about the ref. – Bogdan Muscari Feb 26 '22 at 14:17
0

I'd suggest to use the Fluent Assertions library for this kind of assert checks in unit tests. Instead of rigid equality, you are looking for equivalency.

Then your assert becomes:

input.Should().BeEquivalentTo(expectedRanking);

and this doesn't look for references, only values!

Andrew McClement
  • 1,171
  • 5
  • 14
Mithgroth
  • 1,114
  • 2
  • 13
  • 23