[英]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();
}
}
列表包含此類的對象
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}]
我嘗試了多種方法來比較兩個列表,但都失敗了,例如:- Fluent Assertion - CollectionAssert
當我在調試中檢查兩個 List 時,它們是相同的。 我知道這可能是微不足道的,但我可以在網上找到任何解決方案,提前致謝。
您應該為PointOnChart
類實現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>
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.