大佬教程收集整理的这篇文章主要介绍了NUnit,CollectionAssert.AreEquivalent(…,…),C#Question,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
运行测试时出现以下异常.
using NUnit.Framework; using System.Collections.ObjectModel; public class ExampleClass { public ExampleClass() { Price = 0m; } public String Description { get; set; } public String SKU { get; set; } public decimal Price { get; set; } public int Qty { get; set; } } [TestFixture] public class ExampleClassTests { [Test] public void Exampletest() { var collection1 = new Collection<ExampleClass> { new ExampleClass {Qty = 1,SKU = "971114FT031M"},new ExampleClass {Qty = 1,SKU = "971114FT249LV"} }; var collection2 = new Collection<ExampleClass> { new ExampleClass {Qty = 1,SKU = "971114FT249LV"} }; CollectionAssert.AreEquivalent(collection1,collection2); } }
您可以通过将以下内容添加到ExampleClass来解决此问题
public override bool Equals(object o) { var other = o as ExampleClass; if ( other == null ) { return false; } return this.Description == other.Description && this.SKU == other.SKU && this.Price == other.Price && this.Qty == other.Qty; } public overridE int GetHashCode() { return 1; }
注意:我为GetHashCode选择值1,因为这是一个可变类型,并且可变类型上GetHashCode唯一真正安全的返回值是常量.如果您打算将其用作词典中的键< TKey,TValue>虽然你想重新审视这个.
以上是大佬教程为你收集整理的NUnit,CollectionAssert.AreEquivalent(…,…),C#Question全部内容,希望文章能够帮你解决NUnit,CollectionAssert.AreEquivalent(…,…),C#Question所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。