繁体   English   中英

MSTest - DataTestMethod 抛出异常 System.ArgumentException:“System.Decimal”类型的对象无法转换为“System.Double”类型

[英]MSTest - DataTestMethod throws exception System.ArgumentException: Object of type 'System.Decimal' cannot be converted to type 'System.Double'

我在成功运行的 .NET Core 3.1 测试项目(使用 MSTest)中有一个简单的参数化单元测试。 它看起来像这样:

[DataRow(0.5)]
[DataRow(-1.5)]
[DataTestMethod]
public void MyParameterizedTest(double value)
{
   ...
}

但是在将项目更新到 .NET 5 后,测试失败,并抛出以下异常:

System.ArgumentException: Object of type 'System.Decimal' cannot be converted to type 'System.Double'.
  Stack Trace:
      at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
   at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)

我尝试转换该值,也尝试使用[DynamicData]但发生了相同的异常。

作为最后的手段,我更改了测试以将字符串值转换回双精度值。 它有效,但感觉“hacky”。

[DataRow("0.5")]
[DataRow("-1.5")]
[DataTestMethod]
public void MyParameterizedTest(string stringValue)
{
   var doubleValue = Convert.ToDouble(stringValue);
   ...
}

我如何解决此问题的任何其他建议?

更新相关的 MSTest NuGet 包解决了这个问题,并且我能够使用双精度进行原始参数化测试。

我更新的 NuGet 包是:

  • Microsoft.NET.Test.Sdk( 16.10.0从 16.5.0)
  • MSTest.TestAdapter( 2.2.5从 2.1.0)
  • MSTest.TestFramework( 2.2.5从 2.1.0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM