Home > Mobile >  xUnit tests failing to Assert.Equal two equal values
xUnit tests failing to Assert.Equal two equal values

Time:02-27

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

CodePudding user response:

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

CodePudding user response:

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!

  • Related