【问题标题】:Comparing Collections Fluent Assertions Should All Be Equivalent To Synonyms比较集合流利的断言应该都等同于同义词
【发布时间】:2016-11-08 18:54:43
【问题描述】:

我正在使用 Fluent Assertions 来验证不同的测试对象

public class DTO
{
   public int Key {get; set;}
   public string Code { get; set; }
}

public class KeyDTO
{
   public int Id {get; set;}
   public string Code { get; set; }
}

注意:这不是代码的精确副本,原始 DTO 中有更多字段,但没有必要解释问题

我正在创建一个函数来断言它们是平等的我正在尝试使用流利的断言来做到这一点。我想不出办法说 Id 映射到键。

public void AssertDTOsAreEqual( List<DTO> orderedDTOs, List<KeyDTO> orderedKeys)
{        
    orderedDTOs.ShouldAllBeEquivalentTo(orderedKeys, o => o/*??*/)
}

注意:我知道作为一种替代方法,我可以通过压缩有序集合并比较每个属性来做到这一点,但对于更冗长的 DTO,这将很难为每个属性进行比较。

有谁知道在 ShouldAllBeEquivalentTo 中映射不同属性的方法。或者一般来说可能有更好的方法来做到这一点?

【问题讨论】:

    标签: c# assert fluent-assertions


    【解决方案1】:

    不幸的是还没有。但这是我个人要添加的功能列表中的第一名。我希望尽快得到一些时间。

    【讨论】:

    【解决方案2】:

    subjectCollection.Should().AllBeEquivalentTo(expected) 现已在 FluentAssertions 中实现:

    https://fluentassertions.com/documentation/#collections-and-dictionaries

    抱歉,我误读了这个问题。在当前版本的 FluentAssertions 中我能想到的最好方法是使用 Linq 的 .Select 投影预期的集合并与新对象进行比较:

    subjectCollection.Should().BeEquivalentTo(expectedCollection.Select(o => new { Id = o.Key }));
    

    【讨论】:

    • 同义词图在哪里?
    • 抱歉,我误读了这个问题。我能做的最好的事情是使用 Linq 进行项目:subjectCollection.Should().BeEquivalentTo(expectedCollection.Select(o =&gt; new { Id = o.Key }))
    猜你喜欢
    • 2016-10-19
    • 2013-06-07
    • 2020-02-18
    • 2018-12-01
    • 2021-11-21
    • 2017-09-20
    • 2023-03-27
    • 2013-03-08
    • 2012-02-18
    相关资源
    最近更新 更多