比较列表时Xunit测试失败

发布于 2025-02-11 13:22:25 字数 1723 浏览 1 评论 0原文

这是我的单位测试,并且会失败。 请帮助我解决这个问题。

更新问题:

看来我正在尝试单元测试jsonserializer.deserialize方法。那么这是合法的单位测试吗?

消息:

    Expected got to be equal to {
    JsonFromTo.CMSContacts.ContactsFilter
    {
        cms = "asees", 
        groups = {"jio", "jiso"}
    }, 
    JsonFromTo.CMSContacts.ContactsFilter
    {
        cms = "bsees", 
        groups = {"jto"}
    }
}
, but {
    JsonFromTo.CMSContacts.ContactsFilter
    {
        cms = "asees", 
        groups = {"jio", "jiso"}
    }, 
    JsonFromTo.CMSContacts.ContactsFilter
    {
        cms = "bsees", 
        groups = {"jto"}
    }
}
 differs at index 0.

正在测试的系统:

public IEnumerable<ContactsFilter> GetFilters(string json)
{
    return JsonSerializer.Deserialize<List<ContactsFilter>>(json);
}

public class ContactsFilter
{
    public string cms { get; set; }
    public List<string> groups { get; set; }
}

单元测试:

public class CmsContactsTest
{
    public const string Filters = @"[{""cms"": ""asees"",""groups"": [""jio"",""jiso""]},{""cms"": ""bsees"",""groups"": [""jto""]}]";

    [Fact]
    public void Should_Return_List()
    {
        //arrange
        var want = new List<ContactsFilter>()
        {
            new ContactsFilter()
            {
                cms = "asees",
                groups = new List<string>{ "jio", "jiso" }
            },
            new ContactsFilter()
            {
                cms = "bsees",
                groups = new List<string>{ "jto"}
            }
        };

        var got = new CmsContacts().GetFilters(Filters);
        got.Should().Equal(want);
    }
}

Here is my unit test and it fails with a message.
Kindly help me fix this.

Update Question:

It looks like i am trying to unit test the JsonSerializer.Deserialize method. So is this a legitimate unit test?

Message:

    Expected got to be equal to {
    JsonFromTo.CMSContacts.ContactsFilter
    {
        cms = "asees", 
        groups = {"jio", "jiso"}
    }, 
    JsonFromTo.CMSContacts.ContactsFilter
    {
        cms = "bsees", 
        groups = {"jto"}
    }
}
, but {
    JsonFromTo.CMSContacts.ContactsFilter
    {
        cms = "asees", 
        groups = {"jio", "jiso"}
    }, 
    JsonFromTo.CMSContacts.ContactsFilter
    {
        cms = "bsees", 
        groups = {"jto"}
    }
}
 differs at index 0.

System Under Test:

public IEnumerable<ContactsFilter> GetFilters(string json)
{
    return JsonSerializer.Deserialize<List<ContactsFilter>>(json);
}

public class ContactsFilter
{
    public string cms { get; set; }
    public List<string> groups { get; set; }
}

Unit Test:

public class CmsContactsTest
{
    public const string Filters = @"[{""cms"": ""asees"",""groups"": [""jio"",""jiso""]},{""cms"": ""bsees"",""groups"": [""jto""]}]";

    [Fact]
    public void Should_Return_List()
    {
        //arrange
        var want = new List<ContactsFilter>()
        {
            new ContactsFilter()
            {
                cms = "asees",
                groups = new List<string>{ "jio", "jiso" }
            },
            new ContactsFilter()
            {
                cms = "bsees",
                groups = new List<string>{ "jto"}
            }
        };

        var got = new CmsContacts().GetFilters(Filters);
        got.Should().Equal(want);
    }
}

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

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

发布评论

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

评论(1

清风不识月 2025-02-18 13:22:25

您应该使用 beegivalentto

got.Should().BeEquivalentTo(want);

报价beegivalentto source code

    // Summary:
    //     Asserts that a collection of objects is equivalent to another collection of objects.        
    // ...
    // Remarks:
    //     Objects within the collections are equivalent when both object graphs have equally
    //     named properties with the same value, irrespective of the type of those objects.
    //     Two properties are also equal if one type can be converted to another and the
    //     result is equal. The type of a collection property is ignored as long as the
    //     collection implements System.Collections.Generic.IEnumerable`1 and all items
    //     in the collection are structurally equal. Notice that actual behavior is determined
    //     by the global defaults managed by FluentAssertions.AssertionOptions.

with 均等>等于< /code>,使用其System.Object.Equals(System.Object)比较元素

You should to use BeEquivalentTo

got.Should().BeEquivalentTo(want);

Quoting BeEquivalentTo source code:

    // Summary:
    //     Asserts that a collection of objects is equivalent to another collection of objects.        
    // ...
    // Remarks:
    //     Objects within the collections are equivalent when both object graphs have equally
    //     named properties with the same value, irrespective of the type of those objects.
    //     Two properties are also equal if one type can be converted to another and the
    //     result is equal. The type of a collection property is ignored as long as the
    //     collection implements System.Collections.Generic.IEnumerable`1 and all items
    //     in the collection are structurally equal. Notice that actual behavior is determined
    //     by the global defaults managed by FluentAssertions.AssertionOptions.

With Equal, elements are compared using their System.Object.Equals(System.Object)

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