为什么存在 Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Equals() ?

发布于 2024-07-12 08:07:21 字数 200 浏览 7 评论 0原文

MSDN 文档中对 Assert.Equals() 的描述: 不要使用此方法。

就是这样,完整的解释。 呃..好吧,但是……为什么会在那里? 它是框架早期版本中已弃用的方法吗? 某些东西应该只能由其他 Microsoft 程序集使用?

它只是让我更想使用它,因为我知道我不应该这样做。 ;-)

有人知道吗?

Description for Assert.Equals() from the MSDN Documentation:
Do not use this method.

That's it, the full explanation. Uh.. ok, but then ... why is it there?
Is it a deprecated method from an earlier version of the framework? Something that's supposed to be used only by other Microsoft Assemblies?

It just makes me want to use it all the more knowing I'm not supposed to. ;-)

Does anyone know?

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

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

发布评论

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

评论(4

假扮的天使 2024-07-19 08:07:21

.Equals继承自object。 它被列为“不要使用此方法”,因此用户不会将其与 AreEqual 方法混淆。

.Equals is inherited from object. It's listed as "Do not use this method" so users won't confuse it with the AreEqual method.

耳根太软 2024-07-19 08:07:21

.NET 中的所有对象均派生自 对象

对象有一个 .Equals() 方法。

显然,这个特定对象的 .Equals() 方法没有做任何有用的事情,因此文档警告您它没有做任何有用的事情。

All objects in .NET derive from Object.

Object has a .Equals() method.

Apparently the .Equals() method for this particular object doesn't do anything useful, so the docs are warning you that it doesn't do anything useful.

情场扛把子 2024-07-19 08:07:21

它在 2008 年(也许是 SP1)进行了更改,在调用时无法通过测试,因此意外使用它的人会被告知他们真的不应该使用它。

It was changed in 2008 (Maybe SP1) to fail a test when called, so that people who were using it by accident were told they really shouldn't be using it.

梦言归人 2024-07-19 08:07:21

Assert.Equals,就像它的基类方法 Object.Equals 一样,对于比较对象非常有用。 但是,这两种方法对于独立检测和报告或单元测试中的错误都没有用,因为如果值不相等,Object.Equals 将返回布尔值而不是抛出异常。 如果在单元测试中像这样使用,这是一个问题:

Assert.Equals(42, ComputeMeaningOfLife());

除了这个单元测试可能运行太长时间的问题之外:-),即使 Compute 方法提供错误的结果,这个测试也会默默地成功。 正确使用的方法是 Assert.AreEqual,它不会返回任何内容,但如果参数不相等,则会引发异常。

添加了 Assert.Equals,因此上面示例中的代码不会回退到 Object.Equals 并默默地中和单元测试。 相反,当从单元测试调用时,Assert.Equals 总是抛出异常,提醒您不要使用它。

Assert.Equals, just like its based class method Object.Equals, is perfectly useful for comparing objects. However, neither method is useful for stand-alone detection and reporting or errors in unit testing, since Object.Equals returns a boolean rather than throws if the values are not equal. This is a problem if used like this in a unit test:

Assert.Equals(42, ComputeMeaningOfLife());

Aside from the problem of this unit test possibly running too long :-), this test would silently succeed even if the Compute method provides the wrong result. The right method to use is Assert.AreEqual, which doesn't return anything, but throws an exception if the parameters aren't equal.

Assert.Equals was added so code like in the sample above doesn't fall back to Object.Equals and silently neuter the unit test. Instead, when called from a unit test, Assert.Equals always throws an exception reminding you not to use it.

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