在单元测试中是否应该覆盖等于方法来断言对象相等?

编程入门 行业动态 更新时间:2024-10-27 10:32:33
本文介绍了在单元测试中是否应该覆盖等于方法来断言对象相等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我们通过断言结果对象的所有属性与预期结果对象的属性相等来测试方法的结果.我们是否应该实现 equals 方法并使用 Assert.AreEqual(expectedResult, actualResult)... 但是 equals 在生产代码中可能意味着不同的东西.

Let's say we are testing the result of a method by asserting the equality of all the properties of the result object with properties of an expected result object. Should we implement equals method and use Assert.AreEqual(expectedResult, actualResult)... But equals may mean something different in production code.

最佳实践是什么?

  • 通过覆盖的equals方法断言对象的相等性

  • 断言所有属性的相等
推荐答案

我一次使用自定义断言.主要有两个原因:

I for one use custom assertions. There are two main reasons:

  • 不要将测试问题强加到生产中.这意味着 equals 在测试方法中的含义可能与生产代码的含义不一致;
  • equals 可能对所有测试都不够好.不同的测试需要不同的断言,因此您最终可能会使用自定义断言.
  • don't force test concerns into production. This means that the meaning of equals in a test method might not coincide with the meaning for production code;
  • equals may not be good enough for all the tests. Different tests will require different assertions, so you'll likely end up using custom assertions anyway.

更多推荐

在单元测试中是否应该覆盖等于方法来断言对象相等?

本文发布于:2023-11-03 19:56:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555943.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:断言   方法来   单元   对象   测试中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!