SparkyTestHelpers.Mapping 1.12.0

Unit test helpers for asserting public properties on class instances "mapped" from one type to another (via AutoMapper, another tool, or hand-written code) have the correct values.

Install-Package SparkyTestHelpers.Mapping -Version 1.12.0
dotnet add package SparkyTestHelpers.Mapping --version 1.12.0
<PackageReference Include="SparkyTestHelpers.Mapping" Version="1.12.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SparkyTestHelpers.Mapping --version 1.12.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

see also:


MapTester<TSource, TDestination>

class SparkyTestHelpers.Mapping.MapTester<TSource, TDestination>

This class is for testing that properties were successfully "mapped" from one type to another, either via AutoMapper or another automatic mapping framework, or by hand-written "destination.X = source.X;" code.

See SparkyTestHelpers.AutoMapper for additional extension methods specifically for working with AutoMapper.

Methods

  • MapTester<TSource, TDestination> WithLogging(Action<String> action)
    (optional) "Callback" action to log property values when asserting. If called without an action, defaults to Console.WriteLine.
  • MapTester<TSource, TDestination> IgnoringMember(Expression<Func<TDestination, Object>> destExpression)
  • MapTester<TSource, TDestination> IgnoringMembers(params Expression<Func<TDestination, Object>>[] destExpressions)
  • MapTester<TSource, TDestination> IgnoringMemberNamesStartingWith(string prefix)
  • MapMemberTester<TSource, TDestination> WhereMember(Expression<Func<TDestination, Object>> destExpression)
  • MapMemberTester<TSource, TDestination> WhereMember(Expression<Func<TDestination, Object>> destExpression)
  • void AssertMappedValues(TSource source, TDestination dest)
    throws exception if any source/destination map validation specifications fail, or if any destination properties aren�t either tested or IgnoreMember�d.

Static Methods

  • MapTester<TSource, TDestination> ForMap()

Example

using SparkyTestHelpers.Mapping;
    Foo foo = CreateAndPopulateTestFoo();
    Bar bar = Mapper.Map<Foo, Bar>(foo); 

    MapTester.ForMap<Foo, Bar>()
        .WithLogging()
        .IgnoringMember(dest => dest.DestOnlyProperty)
        .WhereMember(dest => dest.StatusCode).ShouldEqual(src => src.Status)
        .WhereMember(dest => dest.IsValid).ShouldEqualValue(true)
        .WhereMember(dest => dest.Percent).IsTestedBy((src, dest) => 
            Assert.AreEqual(src.Rate / 100, dest.Percent))
        .AssertMappedValues(foo, bar);

You don't have to configure anything for properties with the same name/type in the source and destination instances. AssertMappedValues() considers those successful if the source/destination values match.

Use IgnoringMember, IgnoringMembers or IgnoringMemberNamesStartingWith for destination properties that are not mapped or which you need to test in another way

Use WhereMember to "dot" to the ShouldEqual, ShouldEqualValue and IsTestedBy functions.


MapMemberTester<TSource, TDestination>

class SparkyTestHelpers.Mapping.MapMemberTester<TSource, TDestination>

This class is for testing that a property was successfully "mapped" from one type to another.

Methods

  • MapTester<TSource, TDestination> ShouldEqual(Expression<Func<TSource, Object>> sourceExpression)
    use to verify destination property mapped from differently named source property(s)
  • MapTester<TSource, TDestination> ShouldBeStringMatchFor(Expression<Func<TSource, Object>> sourceExpression)
    use to verify the ".ToString()" values of the source and destination properties (useful for testing Enum mapping where the source/destination Enum types have the same names)
  • MapTester<TSource, TDestination> ShouldEqualValue(Object value)
    use to verify destination property using a constant or some other value not derived from the source
  • MapTester<TSource, TDestination> IsTestedBy(Action<TSource, TDestination> customTest)
    use for custom complex validation that doesn't fit one of the other verification methods

see also:


MapTester<TSource, TDestination>

class SparkyTestHelpers.Mapping.MapTester<TSource, TDestination>

This class is for testing that properties were successfully "mapped" from one type to another, either via AutoMapper or another automatic mapping framework, or by hand-written "destination.X = source.X;" code.

See SparkyTestHelpers.AutoMapper for additional extension methods specifically for working with AutoMapper.

Methods

  • MapTester<TSource, TDestination> WithLogging(Action<String> action)
    (optional) "Callback" action to log property values when asserting. If called without an action, defaults to Console.WriteLine.
  • MapTester<TSource, TDestination> IgnoringMember(Expression<Func<TDestination, Object>> destExpression)
  • MapTester<TSource, TDestination> IgnoringMembers(params Expression<Func<TDestination, Object>>[] destExpressions)
  • MapTester<TSource, TDestination> IgnoringMemberNamesStartingWith(string prefix)
  • MapMemberTester<TSource, TDestination> WhereMember(Expression<Func<TDestination, Object>> destExpression)
  • MapMemberTester<TSource, TDestination> WhereMember(Expression<Func<TDestination, Object>> destExpression)
  • void AssertMappedValues(TSource source, TDestination dest)
    throws exception if any source/destination map validation specifications fail, or if any destination properties aren�t either tested or IgnoreMember�d.

Static Methods

  • MapTester<TSource, TDestination> ForMap()

Example

using SparkyTestHelpers.Mapping;
    Foo foo = CreateAndPopulateTestFoo();
    Bar bar = Mapper.Map<Foo, Bar>(foo); 

    MapTester.ForMap<Foo, Bar>()
        .WithLogging()
        .IgnoringMember(dest => dest.DestOnlyProperty)
        .WhereMember(dest => dest.StatusCode).ShouldEqual(src => src.Status)
        .WhereMember(dest => dest.IsValid).ShouldEqualValue(true)
        .WhereMember(dest => dest.Percent).IsTestedBy((src, dest) => 
            Assert.AreEqual(src.Rate / 100, dest.Percent))
        .AssertMappedValues(foo, bar);

You don't have to configure anything for properties with the same name/type in the source and destination instances. AssertMappedValues() considers those successful if the source/destination values match.

Use IgnoringMember, IgnoringMembers or IgnoringMemberNamesStartingWith for destination properties that are not mapped or which you need to test in another way

Use WhereMember to "dot" to the ShouldEqual, ShouldEqualValue and IsTestedBy functions.


MapMemberTester<TSource, TDestination>

class SparkyTestHelpers.Mapping.MapMemberTester<TSource, TDestination>

This class is for testing that a property was successfully "mapped" from one type to another.

Methods

  • MapTester<TSource, TDestination> ShouldEqual(Expression<Func<TSource, Object>> sourceExpression)
    use to verify destination property mapped from differently named source property(s)
  • MapTester<TSource, TDestination> ShouldBeStringMatchFor(Expression<Func<TSource, Object>> sourceExpression)
    use to verify the ".ToString()" values of the source and destination properties (useful for testing Enum mapping where the source/destination Enum types have the same names)
  • MapTester<TSource, TDestination> ShouldEqualValue(Object value)
    use to verify destination property using a constant or some other value not derived from the source
  • MapTester<TSource, TDestination> IsTestedBy(Action<TSource, TDestination> customTest)
    use for custom complex validation that doesn't fit one of the other verification methods

Release Notes

v1.11: Added SparkyTestHelpers.Populater dependency for random values logic
v1.12: Updated SparkyTestHelpers.Populater dependency to v1.2

Version History

Version Downloads Last updated
1.12.0 1,067 1/15/2019
1.11.0 93 1/3/2019
1.10.2 94 12/12/2018
1.10.1 105 12/2/2018
1.10.0 115 11/22/2018
1.9.0 133 10/20/2018
1.8.0 141 7/31/2018
1.7.2 5,133 6/2/2018
1.7.1 171 5/28/2018
1.7.0 175 5/17/2018
1.6.2 186 5/14/2018
1.6.1 172 5/14/2018
1.6.0 189 5/11/2018
1.5.2 233 5/3/2018
1.5.1 181 5/1/2018
1.5.0 210 4/27/2018
1.4.0 215 4/21/2018
1.3.0 307 4/21/2018
1.2.2 346 3/12/2018
1.2.1 197 2/21/2018
1.2.0 235 2/14/2018
1.0.0 210 2/13/2018