Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem Unit Testing in VB.NET

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()?

like image 466
user489041 Avatar asked Aug 16 '11 14:08

user489041


2 Answers

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.

like image 192
Paul Sasik Avatar answered Sep 28 '22 01:09

Paul Sasik


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.

like image 29
Anders Forsgren Avatar answered Sep 28 '22 01:09

Anders Forsgren