Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
617 views
in Technique[技术] by (71.8m points)

c# - Verifying complete Mapping of an [unordered] Collection/Set of Items in a Unit Test

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
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 ) );
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...