单元测试Assert.AreEqual失败 [英] Unit Test Assert.AreEqual failed

查看:1642
本文介绍了单元测试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.com/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: http://msdn.microsoft.com/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失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆