执行 Assert.AreMatch() 来深度比较两个对象中的属性

发布于 2024-09-14 18:47:32 字数 2668 浏览 11 评论 0原文

我正在针对我们的缓存机制编写测试,我想确保进入缓存的内容与出来的内容相同,即所有属性都匹配。这是一个虚构的示例,说明我希望它如何工作。

    [Test]
    public void add_client_model_member_to_cache_then_retreve()
    {
        //arrange
        MemcachedClient client = new MemcachedClient();
        Member member = GetMember();
        client.Store(StoreMode.Set, "membertest", member);

        // act
        var result = client.Get<Member>("membertest");

        // assert
        Assert.AreMatch(result, member);
    }

对每个属性执行 Assert.AreEqual 是不可行的,因为会有许多这样的测试,每个测试都有许多属性。

哇,谢谢乔恩。我想你在一分钟之内就回答了这个问题。这是我为任何感兴趣的各方提供的解决方案。我按照 Jon 建议的方式实现(我认为),但是我在数组属性方面遇到了一些麻烦,因此我的解决方案仅处理整数数组(我当前需要的所有内容)。

如果我尝试检查比一层更深的地方,它也一定是一个相当滑的斜坡。我确信这是可以实现的,但就我的目的而言,这不是必需的。

private bool AreMatch(object initial, object result)
    {
        if (initial.Equals(result)) 
            return true;

        foreach (var property in initial.GetType().GetProperties())
        {
            var initialPropValue = property.GetValue(initial,null);
            var resultPropValue = result.GetType().GetProperty(property.Name).GetValue(result,null);

            if (property.PropertyType.IsArray)
            {
                if ((initialPropValue != null && resultPropValue != null) && !Enumerable.SequenceEqual((int[])initialPropValue, (int[])resultPropValue))
                        return false;                   
            }
            else if (!object.Equals(initialPropValue, resultPropValue))
            {
                return false;
            }
            else if (initialPropValue != null && property.PropertyType.IsClass)
            {
                // Don't go deeper than one level, this got me into trouble
                //if (!AreMatch(initialPropValue, resultPropValue))
                //  return false;
            }
        }
        return true;
    }

上述方法可用于以下情况

    [Test]
    public void cached_result_is_same_as_original()
    {
        //arrange
        Member member = GetMember();
        client.Store(StoreMode.Set, "membertest", member);

        // act
        var result = client.Get<Member>("membertest");

        // assert
        Assert.IsTrue(AreMatch(member, result));
    }

    [Test]
    public void cached_result_is_same_as_original()
    {
        //arrange
        Member member = GetMember();
        client.Store(StoreMode.Set, "membertest", member);

        // act
        var result = client.Get<Member>("membertest");
        result.FirstName = "Result is different"; 

        // assert
        Assert.IsFalse(AreMatch(member, result));
    }

I am writing tests against our Caching mechanism and I want to be sure that what goes into the cache is the same as what comes out, ie that all properties match. Here is a fictional example of how I would like it to work

    [Test]
    public void add_client_model_member_to_cache_then_retreve()
    {
        //arrange
        MemcachedClient client = new MemcachedClient();
        Member member = GetMember();
        client.Store(StoreMode.Set, "membertest", member);

        // act
        var result = client.Get<Member>("membertest");

        // assert
        Assert.AreMatch(result, member);
    }

It is not feasible to perform Assert.AreEqual on each property as there will be many of these tests with many properties in each.

Wow, thanks Jon. I think you answered that in under one minute. Here are my resulting solution for any interested parties. I implemented as Jon suggested (I think) however I got into a little trouble with array properties and as such my solution only handles arrays of ints (all I currently require).

It also got to be a fairly slippery slope if I try to check deeper than one level. I am sure this can be achieved however for my purposes it is not required.

private bool AreMatch(object initial, object result)
    {
        if (initial.Equals(result)) 
            return true;

        foreach (var property in initial.GetType().GetProperties())
        {
            var initialPropValue = property.GetValue(initial,null);
            var resultPropValue = result.GetType().GetProperty(property.Name).GetValue(result,null);

            if (property.PropertyType.IsArray)
            {
                if ((initialPropValue != null && resultPropValue != null) && !Enumerable.SequenceEqual((int[])initialPropValue, (int[])resultPropValue))
                        return false;                   
            }
            else if (!object.Equals(initialPropValue, resultPropValue))
            {
                return false;
            }
            else if (initialPropValue != null && property.PropertyType.IsClass)
            {
                // Don't go deeper than one level, this got me into trouble
                //if (!AreMatch(initialPropValue, resultPropValue))
                //  return false;
            }
        }
        return true;
    }

The method above can be used in the following situations

    [Test]
    public void cached_result_is_same_as_original()
    {
        //arrange
        Member member = GetMember();
        client.Store(StoreMode.Set, "membertest", member);

        // act
        var result = client.Get<Member>("membertest");

        // assert
        Assert.IsTrue(AreMatch(member, result));
    }

    [Test]
    public void cached_result_is_same_as_original()
    {
        //arrange
        Member member = GetMember();
        client.Store(StoreMode.Set, "membertest", member);

        // act
        var result = client.Get<Member>("membertest");
        result.FirstName = "Result is different"; 

        // assert
        Assert.IsFalse(AreMatch(member, result));
    }

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

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

发布评论

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

评论(1

猫九 2024-09-21 18:47:32

好吧,您当然可以编写一些东西来递归所有属性,并根据从预期值和实际值获取属性值的结果调用 object.Equals。使用 Type.GetProperties() 获取属性本身,并使用 PropertyInfo.GetValue 获取值。

它会有点粗糙,但如果需要的话您可以随时对其进行调整。

Well, you could certainly write something to recurse through all the properties, and call object.Equals on the result of fetching the property value from the expected and actual ones. Use Type.GetProperties() to get the properties themselves, and PropertyInfo.GetValue to get the value.

It'll be somewhat crude, but you could always tweak it if necessary.

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