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