C# 测试 - 比较自定义类型列表
C# Testing - compare list of custom type
我正在尝试编写测试来检查 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();
}
}
列表包含此 class
的对象
public class PointOnChart
{
public string Timestamp { get; set; }
public double Value1 { get; set; }
public double Value2 { get; set; }
public double Value3 { get; set; }
}
这是我要反序列化的文件:
[{"Timestamp":"2020-02-14T09:00:00.000Z","Value1":10,"Value2":20,"Value3":30},
{"Timestamp":"2020-02-14T09:01:00.000Z","Value1":11,"Value2":21,"Value3":31},
{"Timestamp":"2020-02-14T09:02:00.000Z","Value1":12,"Value2":22,"Value3":32},
{"Timestamp":"2020-02-14T09:03:00.000Z","Value1":13,"Value2":23,"Value3":33}]
我尝试了很多方法来比较两个列表,但都失败了,例如:
- 流利的断言
- 集合断言
当我在调试中检查两个列表时,它们是相同的。
我知道这可能是微不足道的,但我可以在网上找到任何解决方案,在此先感谢。
您应该为 PointOnChart
class 实现 Equals
方法,如下所示:
public override bool Equals(object other)
{
if (object.ReferenceEquals(other, this)) return true;
var obj = other as PointOnChart;
if (obj == null) return false;
return this.Timestamp == obj.Timestamp && this.Value1 == obj.Value1 && this.Value2 == obj.Value2 && this.Value3 == obj.Value3;
}
这样 SequenceEquals
扩展方法将正常运行。
与其使用 Equals
覆盖污染您的生产代码,不如考虑使用 www.fluentassertions.com 并将该语句写为:
resultPointsOnChart.Should().BeEquivalentTo(expectedPointsOnChart);
除了其他答案,我建议实施 IEquatable<T>
以及重写 GetHashCode()
和 Equals(Object)
像这样。
public class PointOnChart : IEquatable<PointOnChart> {
public string Timestamp { get; set; }
public double Value1 { get; set; }
public double Value2 { get; set; }
public double Value3 { get; set; }
public override Int32 GetHashCode() => Timestamp.GetHashCode() ^ Value1.GetHashCode() ^ Value2.GetHashCode() ^ Value3.GetHashCode();
public override Boolean Equals(Object obj) => Equals(obj as PointOnChart);
public Boolean Equals(PointOnChart other) => other != null && other.Timestamp == Timestamp && other.Value1.Equals(Value1) && other.Value2.Equals(Value2) && other.Value3.Equals(Value3);
}
这将为您提供所需的所有比较。
如果您以后需要它,还可以更轻松地实现 IEqualityComparer
或 IEqualityComparer<T>
。
我正在尝试编写测试来检查 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();
}
}
列表包含此 class
的对象public class PointOnChart
{
public string Timestamp { get; set; }
public double Value1 { get; set; }
public double Value2 { get; set; }
public double Value3 { get; set; }
}
这是我要反序列化的文件:
[{"Timestamp":"2020-02-14T09:00:00.000Z","Value1":10,"Value2":20,"Value3":30}, {"Timestamp":"2020-02-14T09:01:00.000Z","Value1":11,"Value2":21,"Value3":31}, {"Timestamp":"2020-02-14T09:02:00.000Z","Value1":12,"Value2":22,"Value3":32}, {"Timestamp":"2020-02-14T09:03:00.000Z","Value1":13,"Value2":23,"Value3":33}]
我尝试了很多方法来比较两个列表,但都失败了,例如: - 流利的断言 - 集合断言
当我在调试中检查两个列表时,它们是相同的。 我知道这可能是微不足道的,但我可以在网上找到任何解决方案,在此先感谢。
您应该为 PointOnChart
class 实现 Equals
方法,如下所示:
public override bool Equals(object other)
{
if (object.ReferenceEquals(other, this)) return true;
var obj = other as PointOnChart;
if (obj == null) return false;
return this.Timestamp == obj.Timestamp && this.Value1 == obj.Value1 && this.Value2 == obj.Value2 && this.Value3 == obj.Value3;
}
这样 SequenceEquals
扩展方法将正常运行。
与其使用 Equals
覆盖污染您的生产代码,不如考虑使用 www.fluentassertions.com 并将该语句写为:
resultPointsOnChart.Should().BeEquivalentTo(expectedPointsOnChart);
除了其他答案,我建议实施 IEquatable<T>
以及重写 GetHashCode()
和 Equals(Object)
像这样。
public class PointOnChart : IEquatable<PointOnChart> {
public string Timestamp { get; set; }
public double Value1 { get; set; }
public double Value2 { get; set; }
public double Value3 { get; set; }
public override Int32 GetHashCode() => Timestamp.GetHashCode() ^ Value1.GetHashCode() ^ Value2.GetHashCode() ^ Value3.GetHashCode();
public override Boolean Equals(Object obj) => Equals(obj as PointOnChart);
public Boolean Equals(PointOnChart other) => other != null && other.Timestamp == Timestamp && other.Value1.Equals(Value1) && other.Value2.Equals(Value2) && other.Value3.Equals(Value3);
}
这将为您提供所需的所有比较。
如果您以后需要它,还可以更轻松地实现 IEqualityComparer
或 IEqualityComparer<T>
。