为什么存在 Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Equals() ?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
.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.
.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.
它在 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.
Assert.Equals
,就像它的基类方法Object.Equals
一样,对于比较对象非常有用。 但是,这两种方法对于独立检测和报告或单元测试中的错误都没有用,因为如果值不相等,Object.Equals
将返回布尔值而不是抛出异常。 如果在单元测试中像这样使用,这是一个问题:除了这个单元测试可能运行太长时间的问题之外:-),即使 Compute 方法提供错误的结果,这个测试也会默默地成功。 正确使用的方法是
Assert.AreEqual
,它不会返回任何内容,但如果参数不相等,则会引发异常。添加了
Assert.Equals
,因此上面示例中的代码不会回退到Object.Equals
并默默地中和单元测试。 相反,当从单元测试调用时,Assert.Equals
总是抛出异常,提醒您不要使用它。Assert.Equals
, just like its based class methodObject.Equals
, is perfectly useful for comparing objects. However, neither method is useful for stand-alone detection and reporting or errors in unit testing, sinceObject.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: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 toObject.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.