如何动态选择等效性测试的属性 - fluentAssertions

发布于 2025-01-24 07:44:55 字数 1370 浏览 3 评论 0原文

我正在创建单元测试,其中我将彼此比较对象列表。

目前,我正在使用流利的主张与SpecFlow和Nunit结合使用。我已经使用流利的断言来进行比较如下:

public void TestShizzle()
{
    // I normally retrieve these lists from a moq database or a specflow table
    var expected = list<myObject> 
    {
        new myObject 
        {
            A = 1,
            B = "abc"
        }
    }

    var found = list<myObject> 
    {
        new myObject 
        {
            A = 1,
            B = "def"
        }
    }

    // this comparison only compares a few columns. The comparison is also object dependent. I would like to make this dynamic
    found.Should().BeEquivalentTo(
        expected,
        options =>
        options.Including(x => x.A));
}

我真正想要的是能够使用仿制药而不是指定的类型。我还想决定在编译时比较哪些属性。这是因为数据库中有大量表。我认为我需要为此使用LINQ表达式,但我不知道该怎么做。该函数应该看起来像这样:

public void GenericShizzle<T>(List<T> expected, List<T> found, IEnumerable<PropertyInfo> properties)
{
    Expression<Func<T, object>> principal;
    foreach(var property in properties)
    {
        // create the expression for including fields
    }

    found.Should().BeEquivalentTo(
        expected,
        options =>
        // here is need to apply the expression.
}

我没有真正的想法如何为作业获得正确的表达方式,或者甚至是最佳方法。我认为我需要创建一个属性表达式,该属性表达式可以通过Include函数理解,但是也许可以使用另一种方法?

I'm creating unit tests in which I will be comparing lists of objects with one another.

Currently I am using Fluent assertions in combination with specflow and nunit. I already use the Fluent Assertions to make a comparison as following:

public void TestShizzle()
{
    // I normally retrieve these lists from a moq database or a specflow table
    var expected = list<myObject> 
    {
        new myObject 
        {
            A = 1,
            B = "abc"
        }
    }

    var found = list<myObject> 
    {
        new myObject 
        {
            A = 1,
            B = "def"
        }
    }

    // this comparison only compares a few columns. The comparison is also object dependent. I would like to make this dynamic
    found.Should().BeEquivalentTo(
        expected,
        options =>
        options.Including(x => x.A));
}

What I really want is to be able to use generics instead of a specified type. I also want to decide which properties to compare at compile time. This is because of the large number of tables in the database. I think i need to use Linq Expressions for this, but I don't know how to go about this. The function should look something like this:

public void GenericShizzle<T>(List<T> expected, List<T> found, IEnumerable<PropertyInfo> properties)
{
    Expression<Func<T, object>> principal;
    foreach(var property in properties)
    {
        // create the expression for including fields
    }

    found.Should().BeEquivalentTo(
        expected,
        options =>
        // here is need to apply the expression.
}

I have no real idea how to get the correct expression for the job, or if this even the best method. I think I need to create an property expression that is understood by the include function, but maybe a different method can be used?

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

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

发布评论

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

评论(1

老子叫无熙 2025-01-31 07:44:55

包括方法过载接受expression&lt; func&lt; imemberinfo,bool&gt;&gt;,可用于根据有关它们的信息而动态过滤成员:

IEnumerable<PropertyInfo> properties = ...;
var names = properties
    .Select(info => info.Name)
    .ToHashSet();
found.Should()
    .BeEquivalentTo(expected,
         options => options.Including((IMemberInfo mi) => names.Contains(mi.Name))); // or just .Including(mi => names.Contains(mi.Name))

There is Including method overload accepting Expression<Func<IMemberInfo, bool>> which can be used to dynamically filter members based on information about them:

IEnumerable<PropertyInfo> properties = ...;
var names = properties
    .Select(info => info.Name)
    .ToHashSet();
found.Should()
    .BeEquivalentTo(expected,
         options => options.Including((IMemberInfo mi) => names.Contains(mi.Name))); // or just .Including(mi => names.Contains(mi.Name))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文