单元测试Assert.AreEqual失败

编程入门 行业动态 更新时间:2024-10-11 03:15:58
本文介绍了单元测试Assert.AreEqual失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个单元测试它得到一个对象从集合的方法。这样可以使失败的,我不明白为什么,所以我创建了以下非常简单的测试,创建2个供应商对象,并测试他们是平等的,看看我能在我的code我的测试中发现的问题。但是,本次测试再次失败。任何人都可以看到或解释为什么?

[TestMethod的()     公共无效GetSupplierTest2()     {         供应商预计=新的供应商();         expected.SupplierID = 32532;         expected.SupplierName =测试1         供应商实际=新的供应商();         actual.SupplierID = 32532;         actual.SupplierName =测试1         Assert.AreEqual(预期,实际值);     }

但是,如果我测试测试通过对象的个别属性...

[TestMethod的()     公共无效GetSupplierTest2()     {         供应商预计=新的供应商();         expected.SupplierID = 32532;         expected.SupplierName =测试1     供应商实际=新的供应商();         actual.SupplierID = 32532;         actual.SupplierName =测试1         Assert.AreEqual(expected.SupplierID,actual.SupplierID);         Assert.AreEqual(expected.SupplierName,actual.SupplierName);     }

解决方案

如果你想比较供应商的两个不同的实例,并希望他们被认为是相等时,某些属性具有相同的价值,你必须重写等于方法对公司和方法比较这些属性。

您可以阅读更多关于Equals方法在这里:的http:// MSDN。 microsoft/en-us/library/bsc2ak47.aspx

例如执行:

公众覆盖布尔等于(obj对象) {     如果(obj是供应商)     {         供应商其他=(供应商)目标文件;         返回的equals(other.SupplierID,this.SupplierID)及和放大器;等于(other.SupplierName,this.SupplierName);     }     返回false; }

请注意,你还可以得到一个编译器警告,你必须实现GetHash code为好,这可能是这么简单:

公众覆盖INT GetHash code() {     返回供应商ID; }

I have a unit test for a method which gets an object from a collection. This keeps failing and I cannot see why, so I have created a very simple test below to create 2 supplier object and test they are equal to see if I can spot the problem in my test of my code. But this test again is failing. Can anyone see or explain why?

[TestMethod()] public void GetSupplierTest2() { Supplier expected = new Supplier(); expected.SupplierID = 32532; expected.SupplierName = "Test 1" Supplier actual = new Supplier(); actual.SupplierID = 32532; actual.SupplierName = "Test 1" Assert.AreEqual(expected, actual); }

But if I test the individual properties of the objects the test passes...

[TestMethod()] public void GetSupplierTest2() { Supplier expected = new Supplier(); expected.SupplierID = 32532; expected.SupplierName = "Test 1" Supplier actual = new Supplier(); actual.SupplierID = 32532; actual.SupplierName = "Test 1" Assert.AreEqual(expected.SupplierID , actual.SupplierID ); Assert.AreEqual(expected.SupplierName , actual.SupplierName ); }

解决方案

If you want to compare two different instances of Supplier, and want them to be considered equal when certain properties have the same value, you have to override the Equals method on Supplier and compare those properties in the method.

You can read more about the Equals method here: msdn.microsoft/en-us/library/bsc2ak47.aspx

Example implementation:

public override bool Equals(object obj) { if (obj is Supplier) { Supplier other = (Supplier) obj; return Equals(other.SupplierID, this.SupplierID) && Equals(other.SupplierName, this.SupplierName); } return false; }

Note that you'll also get a compiler warning that you have to implement GetHashCode as well, that could be as simple as this:

public override int GetHashCode() { return SupplierID; }

更多推荐

单元测试Assert.AreEqual失败

本文发布于:2023-10-10 21:56:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479867.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元测试   Assert   AreEqual

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!