Table of Contents
Last updated: 2024-05-17

Testing the Mapper


Caution

Sample code provided in here is for demonstration and educational purposes only which provides customers with programming information regarding the products and is not intended for use in a production environment. All production code should always follow development best practises. All sample code here is supplied "AS IS" without any warranties or support.

This section describes how to use the test features of the Model Framework together with Microsoft's UnitTest project.

  1. Add a new Unit Test Project to the solution and name it Eurostep.Training.Mapper.FamilyToInControl.Test. Make sure to use .NET Core 3.0 or .NET Standard 2.1.
  2. Make sure Prefer 32-bit is unchecked in project Properties > Build
  3. Change Platform target to x64 in Properties > Build for all build configurations
  4. Add NuGet reference to Eurostep.ModelFramework.version.nupkg
  5. Add assembly references to:
    • Eurostep.ModelFramework.Mapping.Testing.dll
    • The generated SoftType toolbox dll, in this example it is Eurostep.Toolbox.InControl.dll
  6. Add Project references to:
    • Eurostep.Training.Mapper.InControlToFamily
    • Eurostep.Training.Toolbox.Family
  7. Set Test -> Test Settings -> Default Processor Architecture to X64.
  8. Change the name of UnitTest1.cs to FamilyUnitTest.cs
  9. Add a new file named FamilyToInControlTestMapping.cs.

Change the implementation of FamilyToInControlTestMapping class to the following:

FamilyToInControlTestMapping.cs

using Eurostep.ModelFramework.Mapping;
using Eurostep.ModelFramework.Mapping.Testing;
using Eurostep.Toolbox.InControl;
using Eurostep.Training.Toolbox.Family;

namespace Eurostep.Training.Mapper.FamilyToInControl.Test
{
    public class FamilyToInControlTestMapping<TMapping> : TestMapping<TMapping, string, IFamilyObject, FamilyRepository, string, SoftTypeBaseObject, SoftTypeRepository>
          where TMapping : IMapping<FamilyRepository, SoftTypeRepository>
    {
    }
}

Open the FamilyUnitTest.cs file and change the implementation to the following:

FamilyUnitTest.cs

using Eurostep.Toolbox.InControl;
using Eurostep.Training.Mapper.FamilyToInControl.Mappings;
using System.Linq;
using Xunit;

using Fam = Eurostep.Training.Toolbox.Family.Model;
using IC = Eurostep.Toolbox.InControl.Model.PersonNamespace;

namespace Eurostep.Training.Mapper.FamilyToInControl.Test
{
    public class FamilyUnitTest
    {
        [Fact]
        public void TestMaleMapping()
        {
            var mapping = new FamilyToInControlTestMapping<MaleToPersonDefaultInMapping>();

            Fam.Male familyMale = new Fam.Male();
            familyMale.FirstName = "MaleFirstName";
            familyMale.LastName = "MaleLastName";
            mapping.Add(familyMale);

            SoftTypeRepository result = mapping.Map();

            Assert.Empty(mapping.Errors);

            var icPersons = result.FindByType<IC.Input.DefaultIn>();
            IC.Input.DefaultIn icPerson = icPersons.FirstOrDefault();

            Assert.NotNull(icPerson);
            Assert.StartsWith("Male", icPerson.id);
            Assert.Equal(familyMale.FirstName, icPerson.firstName);
            Assert.Equal(familyMale.LastName, icPerson.lastName);
        }

        [Fact]
        public void TestFemaleMapping()
        {
            var mapping = new FamilyToInControlTestMapping<FemaleToPersonDefaultInMapping>();

            Fam.Female familyFemale = new Fam.Female();
            familyFemale.FirstName = "FemaleFirstName";
            familyFemale.LastName = "FemaleLastName";
            mapping.Add(familyFemale);

            SoftTypeRepository result = mapping.Map();

            Assert.Empty(mapping.Errors);

            var icPersons = result.FindByType<IC.Input.DefaultIn>();
            IC.Input.DefaultIn icPerson = icPersons.FirstOrDefault();

            Assert.NotNull(icPerson);
            Assert.StartsWith("Female", icPerson.id);
            Assert.Equal(familyFemale.FirstName, icPerson.firstName);
            Assert.Equal(familyFemale.LastName, icPerson.lastName);
        }
    }
}

Clean and rebuild the solution to make sure that everything is OK.

Right click the test method TestMaleMapping and select Run Tests to trigger the test run, then do the same for TestFemaleMapping and if all is well the both tests should pass.

Note

The value for the using alias IC in the code examples above will be different if the generated SoftType toolbox namespace is named differently