Why does Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Equals() exist?

ぐ巨炮叔叔 提交于 2020-01-03 06:50:12

问题


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?


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/489117/why-does-microsoft-visualstudio-testtools-unittesting-assert-equals-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!