NUnit的比较两份名单 [英] NUnit comparing two lists

查看:156
本文介绍了NUnit的比较两份名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

行,所以我是相当新的单元测试和一切都很顺利,直到如今。
我在这里简化了我的问题,但基本上我有以下几点:

OK so I'm fairly new to unit testing and everything is going well until now. I'm simplifying my problem here, but basically I have the following:

[Test]
public void ListTest()
{
    var expected = new List<MyClass>();
    expected.Add(new MyOtherClass());
    var actual = new List<MyClass>();
    actual.Add(new MyOtherClass());
    Assert.AreEqual(expected,actual);
    //CollectionAssert.AreEqual(expected,actual);
}



但测试失败,不应该通过测试?我缺少什么?

But the test is failing, shouldn't the test pass? what am I missing?

推荐答案

我将我的评论回答的请求。

I convert my comment to answer on request.

那么,这将失败,因为 AreEqual 使用参考比较。 ,为了使其工作需要值比较(自定义的比较)

Well, this fails because AreEqual uses reference comparison. In order to make it work you need value comparison(your own custom comparison).

您几乎可以做,通过实施的 IEquatable 接口。并牢记,当你实现了这个接口您必须替换的Object.Equals Object.GetHashCode 以及获得一致的结果。

You can pretty much do that by implementing IEquatable interface. and keep in mind when you're implementing this interface you must override Object.Equals and Object.GetHashCode as well to get consistent results.

的.Net框架支持没有实施这样 IEquatable 你需要的IEqualityComparer 应该做的伎俩,但 NUnit的应该有将其作为重载的方法。我不能确定NUnit的,虽然。

.Net framework supports doing this without implementing IEquatable you need IEqualityComparer that should do the trick, but nunit should have a method which takes this as a overload. Am not certain about "nunit" though.

这篇关于NUnit的比较两份名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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