SparkyTestHelpers.AutoMapper 2.1.1

Additional AutoMapper extension methods for SparkyTestHelpers.Mapping (Unit test helpers for asserting public properties on class instances "mapped" from one type to another have the correct values.)

Version 1.x supports .NET Standard 1.5 and .NET Framework 4.5
Version 2.x supports .NET Standard 2.0 and .NET Framework 4.6

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

see also:


This package extends SparkyTestHelpers.Mapping with some additional extension methods specifically for use with AutoMapper.


MapTester<TSource, TDestination>

class SparkyTestHelpers.Mapping.MapTester<TSource, TDestination>

This class is for testing that properties were successfully "mapped" from one type to another.

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)
  • 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.

Extension Methods for AutoMapper

  • TDestination AssertAutoMappedValues(TSource source)
    calls static Mapper.Map<TSource, TDestination>(source), then AssertMappedValues()
  • TDestination AssertAutoMappedValues(TSource source, IMapper mapper)
    calls IMapper.Map<TSource, TDestination>(source), then AssertMappedValues()
  • TDestination AssertAutoMappedRandomValues()
    uses RandomValuesHelper to create a new TSource instance, calls static Mapper.Map<TSource, TDestination>(source), then AssertMappedValues()
  • TDestination AssertAutoMappedRandomValues(IMapper mapper)
    uses RandomValuesHelper to create a new TSource instance, calls IMapper.Map<TSource, TDestination>(source), then AssertMappedValues()

Static Methods

  • *MapTester<TSource, TDestination> ForMap<TSource, TDestination>

Examples

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

AutoMapper-specific methods:

using SparkyTestHelpers.AutoMapper;
    Baz baz = CreateAndPopulateTestBax();
    Qux qux = MapTester.ForMap<Baz, Qux>.AssertAutoMappedValues(baz);
    // Create & populate Baz, map to Qux, assert success all in one statement!
    MapTester.ForMap<Baz, Qux>.AssertAutoMappedRandomValues();

As with AutoMapper, 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 matxh.

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> 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

RandomValuesHelper

class SparkyTestHelpers.Mapping.RandomValuesHelper

Test helper for updating class instance properties with random values (so you
can fill a "source" instance without writing a lot of code).

Methods

  • T CreateInstanceWithRandomValues()
  • T UpdatePropertiesWithRandomValues(T instance)

Example

using SparkyTestHelpers.Mapping;
    Foo foo = new RandomValuesHelper().CreateInstanceWithRandomValues<Foo>();

    var bar = new Bar();
    new RandomValuesHelper().UpdatePropertiesWithRandomValues(bar);

see also:


This package extends SparkyTestHelpers.Mapping with some additional extension methods specifically for use with AutoMapper.


MapTester<TSource, TDestination>

class SparkyTestHelpers.Mapping.MapTester<TSource, TDestination>

This class is for testing that properties were successfully "mapped" from one type to another.

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)
  • 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.

Extension Methods for AutoMapper

  • TDestination AssertAutoMappedValues(TSource source)
    calls static Mapper.Map<TSource, TDestination>(source), then AssertMappedValues()
  • TDestination AssertAutoMappedValues(TSource source, IMapper mapper)
    calls IMapper.Map<TSource, TDestination>(source), then AssertMappedValues()
  • TDestination AssertAutoMappedRandomValues()
    uses RandomValuesHelper to create a new TSource instance, calls static Mapper.Map<TSource, TDestination>(source), then AssertMappedValues()
  • TDestination AssertAutoMappedRandomValues(IMapper mapper)
    uses RandomValuesHelper to create a new TSource instance, calls IMapper.Map<TSource, TDestination>(source), then AssertMappedValues()

Static Methods

  • *MapTester<TSource, TDestination> ForMap<TSource, TDestination>

Examples

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

AutoMapper-specific methods:

using SparkyTestHelpers.AutoMapper;
    Baz baz = CreateAndPopulateTestBax();
    Qux qux = MapTester.ForMap<Baz, Qux>.AssertAutoMappedValues(baz);
    // Create & populate Baz, map to Qux, assert success all in one statement!
    MapTester.ForMap<Baz, Qux>.AssertAutoMappedRandomValues();

As with AutoMapper, 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 matxh.

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> 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

RandomValuesHelper

class SparkyTestHelpers.Mapping.RandomValuesHelper

Test helper for updating class instance properties with random values (so you
can fill a "source" instance without writing a lot of code).

Methods

  • T CreateInstanceWithRandomValues()
  • T UpdatePropertiesWithRandomValues(T instance)

Example

using SparkyTestHelpers.Mapping;
    Foo foo = new RandomValuesHelper().CreateInstanceWithRandomValues<Foo>();

    var bar = new Bar();
    new RandomValuesHelper().UpdatePropertiesWithRandomValues(bar);

Release Notes

v2.1 - Updated to use SparkyTools.Populater

Version History

Version Downloads Last updated
2.1.1 138 1/15/2019
2.1.0 78 1/15/2019
2.0.1 89 12/2/2018
2.0.0 94 11/22/2018
1.8.1 254 1/15/2019
1.8.0 74 1/15/2019
1.7.1 5,116 6/2/2018
1.7.0 176 5/17/2018
1.6.3 164 5/16/2018
1.6.2 165 5/14/2018
1.6.0 188 5/11/2018
1.5.2 172 5/4/2018
1.5.1 273 4/27/2018
1.5.0 275 4/27/2018
1.4.0 211 4/21/2018
1.3.1 438 4/14/2018
1.3.0 293 4/14/2018
1.2.2 226 3/12/2018
1.2.1 186 2/21/2018
1.2.0 168 2/14/2018
1.0.0 191 2/13/2018