验证一个[无序]集合/组项目的完整映射在单元测试 [英] Verifying complete Mapping of an [unordered] Collection/Set of Items in a Unit Test

查看:155
本文介绍了验证一个[无序]集合/组项目的完整映射在单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用xUnit.net,AutoFixture和SemanticComparison,并希望确认映射的结果。



在单个项目的水平,我也盖



由于




  • 的项目共用一个标识键

  • 我希望做一个比较对两侧

  • 的价值元素,我不关心排序(并且不希望我的说法下重新排序打破)



我如何验证每一个输入项映射到一个且只有一个输出项中的 使用尽可能多的开箱即用的元件部分尽可能DAMP没有干地



灯具:

 类输入
{
公共字符串名称,描述;
}

级输出
{
公共字符串名称,描述,IgnoreThisField;
}



骨架测试:

  [理论,研究机构Autodata] 
无效MappingWorks(SUT映射器,输入[]输入)
{
无功输出= sut.Map(输入);

// TODO断言,每个输入映射
// TODO断言,我们已经没有多余的产出
}


解决方案

给定一个 [非常整齐] FullOuterJoin操作并xUnit.net V2,你可以将它表示为:

 静态无效VerifyFeaturesetFullyMapped(
的IEnumerable<输出>输出,
的IEnumerable<输入>输入)
{
Assert.All(
inputs.FullOuterJoin(产出,
F => F。项目1,R => r.Name,
(X,Y,键)=>新建{
InDescription = x.Item2,
OutDescription = y.Description}),
INOUT =>
Assert.Equal(inout.InDescription,inout.OutDescription));
}


I'm using xUnit.net, AutoFixture and SemanticComparison and want to verify the results of a mapping.

On the individual item level, I'm well covered.

Given

  • The items share an identifying key
  • I want to do a comparison on the value elements on both side
  • I don't care about ordering (and don't want my Assertion to break under re-ordering)

How do I verify that each and every input item maps to one and only one output item in a DAMP yet DRY manner using as much OOTB componentry as possible ?

Fixtures:

class Input
{ 
   public string Name, Description;
}

class Output
{ 
   public string Name, Description, IgnoreThisField;
}

Skeleton Test:

[Theory,AutoData]
void MappingWorks( Mapper sut, Input[] inputs)
{
    var outputs = sut.Map( inputs);

    // TODO assert that every input is mapped
    // TODO assert that we have have no extra outputs
}

解决方案

Given a [very neat] FullOuterJoin operation and xUnit.net V2, you can express it as:

static void VerifyFeaturesetFullyMapped(
    IEnumerable<Output> outputs,
    IEnumerable<Input> inputs )
{
    Assert.All(
        inputs.FullOuterJoin( outputs,
            f => f.Item1, r => r.Name,
            ( x, y, key ) => new { 
                InDescription = x.Item2, 
                OutDescription = y.Description } ),
        inout =>
            Assert.Equal( inout.InDescription, inout.OutDescription ) );
}

这篇关于验证一个[无序]集合/组项目的完整映射在单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆