如何对十进制数使用Assert.Equal()



我必须为转换UoM类创建单元测试,该类将十进制值作为输入和输出值。在Assert.Equal()中使用十进制数字有什么特殊的吗?我的测试失败了这个错误:"信息:断言。AreEqual失败了。预期:& lt; 0.2690288718 (System.Decimal)祝辞。实际:0.26902887 & lt; system . string)(祝辞!">

这是测试:

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

这里是LinearWeightUnitConvert方法

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是命名空间,它是Utils类的ConvertDecimalToString方法的一部分:

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

这是整个转换方法被调用的地方:

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

一个精确比较的基本建议:

decimal expected = 0;
decimal actual;
Assert.True(decimal.TryParse("-0.00", out actual);
Assert.True(decimal.Equals(expected, actual), "Assert equal as decimal failed. Expected "+expected+" but was "+actual);

然后你有四舍五入的问题。首先指定允许舍入误差的百分比或绝对差值。假设epsilon是一个包含允许的绝对错误的小数,断言看起来像:

Assert.InRange(actual, expected-epsilon, expected+epsilon);

相关内容

  • 没有找到相关文章

最新更新