This question already has an answer here:
这个问题在这里已有答案:
- Compare equality between two objects in NUnit 19 answers
比较NUnit 19答案中两个对象之间的相等性
Is there an assertion built into Nunit that checks all properties between 2 objects are the same, without me having to override Equals?
Nunit中是否有一个断言检查2个对象之间的所有属性是否相同,而我不必重写等于?
I'm currently using reflection to Assert each individual property for a pair of objects.
我目前正在使用反射来断言一对对象的每个单独属性。
3 个解决方案
#1
I don't believe there is.
我不相信有。
Assert.AreEqual compares non-numeric types by Equals.
Assert.AreSame checks if they refer to the same object
Assert.AreEqual按等号比较非数字类型。 Assert.AreSame检查它们是否引用同一个对象
#2
You can write framework agnostic asserts using a library called Should. It also has a very nice fluent syntax which can be used if you like fluent interfaces. I had a blog post related to the same.
您可以使用名为Should的库编写框架无关的断言。它还有一个非常好的流利语法,如果你喜欢流畅的界面,可以使用它。我有一篇与之相关的博客文章。
http://nileshgule.blogspot.com/2010/11/use-should-assertion-library-to-write.html
You can two objects and there properties with ShouldBeEquivalentTo
你可以使用ShouldBeEquivalentTo获得两个对象和属性
dto.ShouldBeEquivalentTo(customer);
#3
https://github.com/kbilsted/StatePrinter has been written specifically to dump object graphs to string representation with the aim of writing easy unit tests.
https://github.com/kbilsted/StatePrinter专门用于将对象图转储为字符串表示,目的是编写简单的单元测试。
- It comes witg Assert methods that output a properly escaped string easy copy-paste into the test to correct it.
- It allows unittest to be automatically re-written
- It integrates with all unit testing frameworks
- Unlike JSON serialization, circular references are supported
- You can easily filter, so only parts of types are dumped
它提供了Assert方法,可以将正确转义的字符串轻松复制粘贴到测试中以进行纠正。
它允许自动重写单元测试
它集成了所有单元测试框架
与JSON序列化不同,支持循环引用
您可以轻松过滤,因此只会转储部分类型
Given
class A
{
public DateTime X;
public DateTime Y { get; set; }
public string Name;
}
You can in a type safe manner, and using auto-completion of visual studio include or exclude fields.
您可以以类型安全的方式,并使用visual studio的自动完成包含或排除字段。
var printer = new Stateprinter();
printer.Configuration.Projectionharvester().Exclude<A>(x => x.X, x => x.Y);
var sut = new A { X = DateTime.Now, Name = "Charly" };
var expected = @"new A(){ Name = ""Charly""}";
printer.Assert.PrintIsSame(expected, sut);
#1
I don't believe there is.
我不相信有。
Assert.AreEqual compares non-numeric types by Equals.
Assert.AreSame checks if they refer to the same object
Assert.AreEqual按等号比较非数字类型。 Assert.AreSame检查它们是否引用同一个对象
#2
You can write framework agnostic asserts using a library called Should. It also has a very nice fluent syntax which can be used if you like fluent interfaces. I had a blog post related to the same.
您可以使用名为Should的库编写框架无关的断言。它还有一个非常好的流利语法,如果你喜欢流畅的界面,可以使用它。我有一篇与之相关的博客文章。
http://nileshgule.blogspot.com/2010/11/use-should-assertion-library-to-write.html
You can two objects and there properties with ShouldBeEquivalentTo
你可以使用ShouldBeEquivalentTo获得两个对象和属性
dto.ShouldBeEquivalentTo(customer);
#3
https://github.com/kbilsted/StatePrinter has been written specifically to dump object graphs to string representation with the aim of writing easy unit tests.
https://github.com/kbilsted/StatePrinter专门用于将对象图转储为字符串表示,目的是编写简单的单元测试。
- It comes witg Assert methods that output a properly escaped string easy copy-paste into the test to correct it.
- It allows unittest to be automatically re-written
- It integrates with all unit testing frameworks
- Unlike JSON serialization, circular references are supported
- You can easily filter, so only parts of types are dumped
它提供了Assert方法,可以将正确转义的字符串轻松复制粘贴到测试中以进行纠正。
它允许自动重写单元测试
它集成了所有单元测试框架
与JSON序列化不同,支持循环引用
您可以轻松过滤,因此只会转储部分类型
Given
class A
{
public DateTime X;
public DateTime Y { get; set; }
public string Name;
}
You can in a type safe manner, and using auto-completion of visual studio include or exclude fields.
您可以以类型安全的方式,并使用visual studio的自动完成包含或排除字段。
var printer = new Stateprinter();
printer.Configuration.Projectionharvester().Exclude<A>(x => x.X, x => x.Y);
var sut = new A { X = DateTime.Now, Name = "Charly" };
var expected = @"new A(){ Name = ""Charly""}";
printer.Assert.PrintIsSame(expected, sut);