0

I have to create unit tests for a convert UoM class, which has decimal values as input and output values. Is there any specific with using decimal numbers with Assert.Equal()? My test fails with this error: "Message: Assert.AreEqual failed. Expected:<0.2690288718 (System.Decimal)>. Actual:<0.26902887 (System.String)>."

This is the test:

[TestMethod]
        public void ValidateConversionKG_FTtoKG_M()
        {
            decimal actualValuetoConvert = 0.082M;
            string actualFromUnit = "kg/ft";
            string actualToUnit = "kg/m";
            var actualResult = ESPv2MigrationTool.CommonTools.Utils.ConvertDecimalToString(ESPv2MigrationTool.CommonTools.Conversion
                .LinearWeightUnitConvert(actualValuetoConvert, actualFromUnit, actualToUnit));
            decimal expectedResult = actualValuetoConvert * 3.2808399M;

            Assert.AreEqual(expectedResult, actualResult);
        }

And here is the LinearWeightUnitConvert method:

 public static decimal LinearWeightUnitConvert(decimal valueToConvert, string fromUnit, string toUnit)
        {
            //kg/m, lb/in, oz/in, g/m

            if (fromUnit.ToUpper() == "KG/M")
            {
                if (toUnit.ToUpper() == "KG/M")
                    return valueToConvert;

                if (toUnit.ToUpper() == "LB/IN")
                    return valueToConvert * 0.0559974146M; //86.79617M
                if (toUnit.ToUpper() == "OZ/IN")
                    return valueToConvert * 0.895958634M; //1388.73866M

                if (toUnit.ToUpper() == "G/M")
                    return valueToConvert * 1000;
            }

ESPv2MigrationTool.CommonTools is the namespace and this is a part of the ConvertDecimalToString method of the Utils class:

public static decimal ConvertStringToDecimal(String inputValue)
        {
            string localValue = null;
            decimal decValue = 0.0M;
            try
            {
                if (!string.IsNullOrEmpty(inputValue))
                {
                    decValue = Decimal.Parse(inputValue, System.Globalization.NumberStyles.AllowExponent | System.Globalization.NumberStyles.Float);
                    localValue = decValue.ToString("#0.########", CultureInfo.InvariantCulture);
                }
            }

And this is where the whole conversion method is invoked:

internal static string ConvertValueOfLinearWeight( DataRow dataRow, EED_Data eedData)
        {
            decimal value = 0.0M;
            if (!string.IsNullOrEmpty(dataRow[HeaderID.LineicWeight].ToString()))
            {
                value = Utils.ConvertStringToDecimal(dataRow[HeaderID.LineicWeight].ToString());
            }

            var fromUnit = CommonMapping.GetUnit(dataRow[HeaderID.DivisionManagementUnit].ToString());
            var toUnit = CommonMapping.GetEedLinearWeightUnit((Units)eedData.Items[0]);

            return Utils.ConvertDecimalToString(Conversion.LinearWeightUnitConvert(value, fromUnit, toUnit));
        }
RRuseva
  • 25
  • 5
  • https://stackoverflow.com/a/8081911/10339675 – JL0PD Oct 25 '21 at 11:28
  • 8
    You are comparing decimal with string (as error message mentions). – Evk Oct 25 '21 at 11:29
  • right...so bad of me that I didn't saw it – RRuseva Oct 25 '21 at 11:35
  • still after changing it to string - i get the same message – RRuseva Oct 25 '21 at 11:36
  • Well then your test actually fails, so either actual code or expectation is wrong, but we cannot help since no code is provided. – Evk Oct 25 '21 at 11:38
  • posted it in the question – RRuseva Oct 25 '21 at 11:42
  • You posted it partially, and that part does not include the conversion actually used in the question (kg/ft > kg/m). Also `SPv2MigrationTool.CommonTools.Utils.ConvertDecimalToString` is not present (I guess it's not just `ToString()` call) – Evk Oct 25 '21 at 11:45
  • I hope that this new edit is specific enough – RRuseva Oct 25 '21 at 11:56
  • It's not, but anyway you likely need to apply your `ESPv2MigrationTool.CommonTools.Utils.ConvertDecimalToString` to your `expectedResult`. Otherwise your actual result is converted to string via some custom function, and your expected result is converted via regular `ToString`. – Evk Oct 25 '21 at 12:01
  • Still thank you for the comments - they were very helpful – RRuseva Oct 25 '21 at 12:07

0 Answers0