我有以下代码: TestMethod() _Public Sub GetDirectoryEntryTest() Dim path As String = runner.getLDAPPath() Dim expected As DirectoryEntry = runner.GetDirectoryEntry() Dim actual As DirectoryEntry actual = LDAPBase.GetDirectoryEntry(pa
<TestMethod()> _ Public Sub GetDirectoryEntryTest() Dim path As String = runner.getLDAPPath() Dim expected As DirectoryEntry = runner.GetDirectoryEntry() Dim actual As DirectoryEntry actual = LDAPBase.GetDirectoryEntry(path) Assert.AreEqual(expected, actual) End Sub
此单元测试失败. DirectoryEntry对象完全相同,但对不同对象的引用不同.我来自Java背景,你总是有.equals().
我能做什么才能正确评估并返回true,因为对于所有意图和目的,对象是相同的.有什么我可以像在Java中那样做并覆盖equals()吗?
尝试将对象的路径与以下内容进行比较:Assert.AreEqual(expected.Path, actual.Path)
这将比较底层路径(字符串类型)而不是对象引用.如果路径相同就足够了,你就不必重写任何东西.
编辑:
DirectoryEntry是一个从Object继承Equals的引用类型,因此:
从Object.Equals Method开始:
The default implementation of Equals supports reference equality for reference types, and bitwise equality for value types. Reference equality means the object references that are compared refer to the same object. Bitwise equality means the objects that are compared have the same binary representation.