NUnit - 断言检查所有属性是否相等?

发布于 2024-07-27 02:04:42 字数 92 浏览 3 评论 0原文

Nunit 中是否内置了一个断言,可以检查两个对象之间的所有属性是否相同,而无需覆盖 Equals?

我目前正在使用反射来断言一对对象的每个单独属性。

Is there an assertion built into Nunit that checks all properties between 2 objects are the same, without me having to override Equals?

I'm currently using reflection to Assert each individual property for a pair of objects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

养猫人 2024-08-03 02:04:42

我不相信有。

Assert.AreEqual 通过 Equals 比较非数字类型。
Assert.AreSame 检查它们是否引用同一个对象

I don't believe there is.

Assert.AreEqual compares non-numeric types by Equals.
Assert.AreSame checks if they refer to the same object

我的奇迹 2024-08-03 02:04:42

您可以使用名为 Should 的库编写与框架无关的断言。 它还具有非常好的流畅语法,如果您喜欢流畅的界面,可以使用它。 我有一篇与此相关的博客文章。

http://nileshgule.blogspot.com/ 2010/11/use-should-assertion-library-to-write.html

您可以使用两个对象和属性 ShouldBeEquivalentTo

dto.ShouldBeEquivalentTo(customer);

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.

http://nileshgule.blogspot.com/2010/11/use-should-assertion-library-to-write.html

You can two objects and there properties with ShouldBeEquivalentTo

dto.ShouldBeEquivalentTo(customer);
提笔书几行 2024-08-03 02:04:42

https://github.com/kbilsted/StatePrinter 专门用于将对象图转储为字符串表示形式编写简单的单元测试的目的。

  • 它带有断言方法,可以输出正确转义的字符串,轻松复制粘贴到测试中以纠正它。
  • 它允许自动重写单元测试
  • 它与所有单元测试框架集成
  • 与JSON序列化不同,支持循环引用
  • 您可以轻松过滤,因此只有部分类型被转储

给定

class A
{
  public DateTime X;
  public DateTime Y { get; set; }
  public string Name;
}

您可以以类型安全的方式,并使用自动完成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);

https://github.com/kbilsted/StatePrinter has been written specifically to dump object graphs to string representation with the aim of writing easy unit tests.

  • 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

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.

  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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文