mardi 19 janvier 2021

C# Assert.AreEqual with dates shows different dates

I have this class:

using System;
using System.Collections.Generic;
using DDDSample1.Domain.Drivers;

namespace DDDSample1.Domain.Drivers
{
    public class DriverMapper
    {
        public static DriverDto toDTO(CreatingDriverDto obj)
        {

            List<string> driverTypes = new List<string>(obj.DriverTypes);
            DateTime start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            DateTime dateBirth = start.AddMilliseconds(obj.DateBirth).ToLocalTime();
            DateTime drivingLicenseExpirationDate = start.AddMilliseconds(obj.DrivingLicenseExpirationDate).ToLocalTime();
            DateTime entryDateCompany = start.AddMilliseconds(obj.EntryDateCompany).ToLocalTime();
            DateTime departureDateCompany = start.AddMilliseconds(obj.DepartureDateCompany).ToLocalTime();
            return new DriverDto(obj.MechanographicNumber, obj.Name, dateBirth, obj.CitizenCardNumber, obj.NIF, obj.DrivingLicenseNumber, drivingLicenseExpirationDate, driverTypes, entryDateCompany, departureDateCompany);
        }
    }
}

And I want to test toe return of the toDto method, that is a DriverDto, as follows:

using System;
using System.Collections.Generic;

namespace DDDSample1.Domain.Drivers
{
    public class DriverDto
    {

        public string Id { get; set; }
        public string MechanographicNumber { get; set; }
        public string Name { get; set; }
        public DateTime DateBirth { get; set; }
        public int CitizenCardNumber { get; set; }
        public int NIF { get; set; }
        public int DrivingLicenseNumber { get; set; }
        public DateTime DrivingLicenseExpirationDate { get; set; }
        public List<string> DriverTypes { get; set; }
        public DateTime EntryDateCompany { get; set; }
        public DateTime DepartureDateCompany { get; set; }

        public DriverDto(string mechanographicNumber, string name, DateTime dateBirth, int citizenCardNumber, int NIF, int drivingLicenseNumber, DateTime drivingLicenseExpirationDate, List<string> driverTypes, DateTime entryDateCompany, DateTime departureDateCompany)
        {

            this.DriverTypes = new List<string>();
            this.MechanographicNumber = mechanographicNumber;
            this.Name = name;
            this.DateBirth = dateBirth;
            this.CitizenCardNumber = citizenCardNumber;
            this.NIF = NIF;
            this.DrivingLicenseNumber = drivingLicenseNumber;
            this.DrivingLicenseExpirationDate = drivingLicenseExpirationDate;
            this.DriverTypes = driverTypes;
            this.EntryDateCompany = entryDateCompany;
            this.DepartureDateCompany = departureDateCompany;
        }
    }
}

And in my unit testing I do the same:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DDDSample1.Domain.Drivers;
using System.Collections.Generic;
using DDDSample1.Domain.Shared;

namespace Tests
{
    [TestClass]
    public class DriverMapperTest
    {
        [TestMethod]
        public void testToDto()
        {
            String mNumber = "12a45678b";
            String name = "DriverTest";
            DateTime birthDate = new DateTime(2000, 06, 14, 0, 0, 0, DateTimeKind.Utc).ToLocalTime();
            int cardNumber = 12345678;
            int NIF = 987654321;
            int drivingLicense = 123456789;
            DateTime drivingLicenseExpirationDate = new DateTime(2024, 05, 01, 0, 0, 0, DateTimeKind.Utc).ToLocalTime();
            List<String> DriverTypes = new List<String> { "Linguas", "Experiente" };
            DateTime EntryDateCompany = new DateTime(2018, 07, 01, 0, 0, 0, DateTimeKind.Utc).ToLocalTime();
            DateTime DepartureDateCompany = new DateTime(2019, 01, 01, 0, 0, 0, DateTimeKind.Utc).ToLocalTime();

            long birthDateMiliseconds = (long)(new TimeSpan(birthDate.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).Ticks)).TotalMilliseconds - 3600000;
            long drivingLicenseExpirationDateMiliseconds = (long)(new TimeSpan(drivingLicenseExpirationDate.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).Ticks)).TotalMilliseconds - 3600000;
            long EntryDateCompanyMiliseconds = (long)(new TimeSpan(EntryDateCompany.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).Ticks)).TotalMilliseconds - 3600000;
            long DepartureDateCompanyMiliseconds = (long)(new TimeSpan(DepartureDateCompany.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).Ticks)).TotalMilliseconds;

            CreatingDriverDto cdto = new CreatingDriverDto(mNumber, name, birthDateMiliseconds, cardNumber, NIF, drivingLicense, drivingLicenseExpirationDateMiliseconds, DriverTypes.ToArray(), EntryDateCompanyMiliseconds, DepartureDateCompanyMiliseconds);

            DriverDto dto = new DriverDto(mNumber, name, birthDate, cardNumber, NIF, drivingLicense, drivingLicenseExpirationDate, DriverTypes, EntryDateCompany, DepartureDateCompany);

            DriverDto dtoMapper = DriverMapper.toDTO(cdto);

            Assert.AreEqual(dto.MechanographicNumber, dtoMapper.MechanographicNumber);
            Assert.AreEqual(dto.Name, dtoMapper.Name);
            // Assert.AreEqual(dto.DateBirth, dtoMapper.DateBirth);
            Assert.AreEqual(dto.CitizenCardNumber, dtoMapper.CitizenCardNumber);
            Assert.AreEqual(dto.NIF, dtoMapper.NIF);
            Assert.AreEqual(dto.DrivingLicenseNumber, dtoMapper.DrivingLicenseNumber);
            // Assert.AreEqual(dto.EntryDateCompany, dtoMapper.EntryDateCompany);
            // Assert.AreEqual(dto.DepartureDateCompany, dtoMapper.DepartureDateCompany);
            // Assert.AreEqual(dto.DrivingLicenseExpirationDate, dtoMapper.DrivingLicenseExpirationDate);
        }
    }
}

The problem is the hours output are not the same, it says that the date I hardcoded in the test class is 14th of june 2000 01h00m but the actual date is 14th of june 2000 02h00m.

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire