xUnit 测试未能 Assert.Equal 两个相等的值

发布于 2025-01-10 13:58:26 字数 2292 浏览 0 评论 0原文

我正在尝试对函数进行 xUnit 测试,但失败了。使用调试器,所有值都符合预期。预期结果与实际结果相等。因此,在下一步中,试图找出失败的原因,我为输入和预期结果分配了相同的值(不使用我想测试的函数)以确保两者相等,然后使用函数 Asser.Equal (expectedResult, input) 但我再次遇到错误(实际结果和预期结果不一样),即使我使它们完全相同。我试图进行的测试是针对结构的。下一步,我没有为预期结果分配值,而是将预期结果=输入并使用Assert.Equal(expectedResult, input),现在测试成功了。我的问题是,为什么当我将值单独分配给预期结果时它不起作用,为什么当我使用输入创建预期结果 = 时它起作用。这是因为代码中的结构吗?

    [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);

    }

如果我这样做它就会起作用 ->

[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);

    }

我尝试测试的函数与代码的第一个示例具有相同的功能,因此我为了示例而键入了值。抱歉,如果我错过了一些信息,请提前感谢您

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风追烟花雨 2025-01-17 13:58:27

我猜想 Contest 不会实现 IEquatable 或以其他方式覆盖 Equals(object)。因此,Assert.Equal 仅当对象引用相等时才起作用。如果您不想这样做,可以创建自己的 IEqualityComparer 并使用重载 Assert.Equal(expectedRanking, input, equalityComparer)。祝你好运

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

Oo萌小芽oO 2025-01-17 13:58:27

我建议使用 Fluent Assertions 库在单元测试中进行此类断言检查。您正在寻找的是等价性,而不是严格的平等。

然后你的断言就变成了:

input.Should().BeEquivalentTo(expectedRanking);

这不寻找引用,只寻找值!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文