【发布时间】:2020-06-03 12:53:38
【问题描述】:
我正在尝试编写测试检查 JSON 转换器是否正确反序列化输入到我的自定义列表
[TestMethod]
public void JSONInput_Changed()
{
List<PointOnChart> _expectedPointsOnChart;
_expectedPointsOnChart = new List<PointOnChart>();
_expectedPointsOnChart.Add(new PointOnChart { Timestamp = "2020-02-14T09:00:00.000Z", Value1 = 10, Value2 = 20, Value3 = 30 });
_expectedPointsOnChart.Add(new PointOnChart { Timestamp = "2020-02-14T09:01:00.000Z", Value1 = 11, Value2 = 21, Value3 = 31 });
_expectedPointsOnChart.Add(new PointOnChart { Timestamp = "2020-02-14T09:02:00.000Z", Value1 = 12, Value2 = 22, Value3 = 32 });
_expectedPointsOnChart.Add(new PointOnChart { Timestamp = "2020-02-14T09:03:00.000Z", Value1 = 13, Value2 = 23, Value3 = 33 });
MultipleBarChart multipleBarChartTest = new MultipleBarChart();
multipleBarChartTest.MeInitialize(DateTimeIntervalType.Minutes);
string JSONstring = System.IO.File.ReadAllText(@"C:\Users\slawomirk\source\repos\VIXCharts\iFixMultipleBarChartTests\TestJson.txt");
multipleBarChartTest.JSONInput = JSONstring;
List<PointOnChart> resultPointsOnChart = multipleBarChartTest.PointsOnChart;
//bool areEqual = _expectedPointsOnChart.SequenceEqual(resultPointsOnChart);
IEnumerable<PointOnChart> resultList;
resultList = _expectedPointsOnChart.Except(resultPointsOnChart);
if (resultList.Any())
{
Assert.Fail();
}
}
列表包含该类的对象
public class PointOnChart
{
public string Timestamp { get; set; }
public double Value1 { get; set; }
public double Value2 { get; set; }
public double Value3 { get; set; }
}
这是我正在阅读以反序列化的文件:
[{"时间戳":"2020-02-14T09:00:00.000Z","Value1":10,"Value2":20,"Value3":30}, {"时间戳":"2020-02-14T09:01:00.000Z","Value1":11,"Value2":21,"Value3":31}, {"时间戳":"2020-02-14T09:02:00.000Z","Value1":12,"Value2":22,"Value3":32}, {"时间戳":"2020-02-14T09:03:00.000Z","Value1":13,"Value2":23,"Value3":33}]
我尝试了多种方法来比较两个列表,但都失败了,例如: - 流利的断言 - 集合断言
当我在调试中检查两个列表时,它们是相同的。 我知道这可能很简单,但我可以在网上找到任何解决方案,在此先感谢。
【问题讨论】:
-
您是否尝试过使用自定义 IEqualityComparer 的SequenceEquals overload?由于您尚未在
PointOnChart本身中定义确定相等性的方法,因此测试框架将无法判断两者是否相同。 -
PointOnChart长什么样子?
标签: c# unit-testing assert fluent-assertions