Comparison of similar objects in unit tests

What is the best way to compare two similar objects?

Given FlintlockDTOand Flintlock:

public class FlintlockDTO
{
  public string GName { get; set; }

  public string SharedPropertyName { get; set; }

  ...
}

and

public class Flintlock
{
  public Flintlock(FlintlockDTO inflator)
  {
    this.GoodName = inflator.GName;
    this.SharedPropertyName = inflator.SharedPropertyName;
    ...
  }

  public string GoodName { get; private set; }

  public string SharedPropertyName { get; private set; }
  ...
}

If both classes have common properties of N (for example, SharedPropertyName), but differ in properties of M, which are equivalent, but are called differently (for example, GoodName\ GName.)

Tools like fluentassert almost do this, if the property names match, to my understanding this will work:

flintlockDto.ShouldBeEquivalentTo(flintlock);

Is there a way to do this neatly in fluentassert or any other tool?

Perfectly,

flintlockDto.IsTheSameAs(flintlock).WhenMapping("GName","GoodName");
+4
source share
4 answers

Likeness, StriplingWarrior. nuget.

:

using NUnit.Framework;
using Ploeh.SemanticComparison;
using Ploeh.SemanticComparison.Fluent;

namespace Tests
{
    [TestFixture]
    class Tests2
    {
        [Test]
        public void ObjectsShuldEqual()
        {
            var flintlockDto = new FlintlockDTO()
            {
                GName = "name",
                AdditionalProperty = "whatever",
                SharedPropertyName = "prop name"
            };
            var flintlock = new Flintlock(flintlockDto);

            Likeness<Flintlock, FlintlockDTO> flintFlockDtoLikeness = flintlock
                .AsSource().OfLikeness<FlintlockDTO>()
                .With(dto => dto.GName).EqualsWhen((flintlock1, dto) => flintlock1.GoodName == dto.GName) // you can write an extension method to encapsulate it
                .Without(dto => dto.AdditionalProperty);

            // assert
            flintFlockDtoLikeness.ShouldEqual(flintlockDto);
        }
    }

    public class FlintlockDTO
    {
        public string GName { get; set; }

        public string SharedPropertyName { get; set; }

        public string AdditionalProperty { get; set; }
    }

    public class Flintlock
    {
        public Flintlock(FlintlockDTO inflator)
        {
            this.GoodName = inflator.GName;
            this.SharedPropertyName = inflator.SharedPropertyName;
        }

        public string GoodName { get; private set; }

        public string SharedPropertyName { get; private set; }
    }
}

:

  • , ( , )
  • ,
+2

, , , , - , :

Assert.AreEqual(
    new{flintlockDto.GoodName, flintlockDto.SharedPropertyName},
    new{GoodName = flintlock.GName, flintlock.SharedPropertyName});

- . Equals() , ToString() , , , .

Mark Seemann Likeness :

, ? AutoFixture Likeness. TSource TDestination Equals.

... ...

+2

/ , .

static class Extensions
{
  public static bool IsEqualTo(this FlintlockDTO expected, Flintlock actual) 
  {
    return expected.GName == actual.GoodName && expected.SharedPropertyName == actual.SharedPropertyName;
  }
}

:

Assert.IsTrue(expected.IsEqualTo(actual));

.

+1
source

It may be good to have FlintlockDTOimplement IEquatable<Flintlock>as follows:

public override Equals(Flintlock other)
{
    return GName == other.GoodName; // modify as necessary (case, culture, etc.)
}

(And don't forget to override and implement correctly GetHashCode.)

Then you can just check

flintlockDto.Equals(flintlock)

as part of unit testing of your choice.

0
source

Source: https://habr.com/ru/post/1598413/


All Articles