在NUnit的两个物体之间比较平等 [英] Compare equality between two objects in NUnit

查看:169
本文介绍了在NUnit的两个物体之间比较平等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图断言一个对象是平等到另一个对象。

I'm trying to assert that one object is "equal" to another object.

的对象是与一群公共属性的一个类的实例只。有没有一种简单的方法有NUnit的基础上,性能主张平等?

The objects are just instances of a class with a bunch of public properties. Is there an easy way to have NUnit assert equality based on the properties?

这是我目前的解决办法,但我觉得有可能是更好的东西:

This is my current solution but I think there may be something better:

Assert.AreEqual(LeftObject.Property1, RightObject.Property1)
Assert.AreEqual(LeftObject.Property2, RightObject.Property2)
Assert.AreEqual(LeftObject.Property3, RightObject.Property3)
...
Assert.AreEqual(LeftObject.PropertyN, RightObject.PropertyN)

我要什么,会以同样的精神作为CollectionEquivalentConstraint其中NUnit的验证两个集合的内容是相同的。

What I'm going for would be in the same spirit as the CollectionEquivalentConstraint wherein NUnit verifies that the contents of two collections are identical.

推荐答案

覆盖.Equals为对象,并在单元测试,那么你可以简单地做到这一点:

Override .Equals for your object and in the unit test you can then simply do this:

Assert.AreEqual(LeftObject, RightObject);

当然,这可能意味着你只需将所有的.Equals方法个体比较,但它将使您可以重用多个测试,实施,很可能是有道理的,如果有物体应该能够将自己与比较反正兄弟姐妹

Of course, this might mean you just move all the individual comparisons to the .Equals method, but it would allow you to reuse that implementation for multiple tests, and probably makes sense to have if objects should be able to compare themselves with siblings anyway.

这篇关于在NUnit的两个物体之间比较平等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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