I have the following code:
<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
This unit test fails. The DirectoryEntry
objects are exactly the same, but different references to different objects. I come from a Java background where you always have the .equals()
.
What can I do so that this will evaluate correctly and return true since for all intents and purposes, the objects are equal. Is there something I can do like I would do in Java and override the equals()?
Try comparing the paths of the objects with something like this:
Assert.AreEqual(expected.Path, actual.Path)
That will compare the underlying paths (string type) rather than the object references. If the paths being the same is enough you shouldn't have to override anything.
EDIT:
DirectoryEntry is a reference type that inherits Equals from Object and so:
From 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.
In what sense are the two entries equal? Are their ToString() equal? In case that is enough to consider them equal in the test, then compare the two ToString() values to eachother.
If you need to compare the two DirectoryEntries, implement a custom IEqualityComparer and use that to compare for equality in the test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With