VB.NET如何编写高效的问题单元测试?

2026-05-06 08:471阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计318个文字,预计阅读时间需要2分钟。

VB.NET如何编写高效的问题单元测试?

我有一段伪代码如下:plaintextTestMethod() + Public Sub GetDirectoryEntryTest() Dim path As String=runner.getLDAPPath() Dim expected As DirectoryEntry=runner.GetDirectoryEntry() Dim actual As DirectoryEntry=LDAPBase.GetDirectoryEntry(path)简化后的内容:plaintext在GetDirectoryEntryTest方法中,获取LDAP路径,比较预期与实际DirectoryEntry对象。

我有以下代码:

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

尝试将对象的路径与以下内容进行比较:

VB.NET如何编写高效的问题单元测试?

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.

本文共计318个文字,预计阅读时间需要2分钟。

VB.NET如何编写高效的问题单元测试?

我有一段伪代码如下:plaintextTestMethod() + Public Sub GetDirectoryEntryTest() Dim path As String=runner.getLDAPPath() Dim expected As DirectoryEntry=runner.GetDirectoryEntry() Dim actual As DirectoryEntry=LDAPBase.GetDirectoryEntry(path)简化后的内容:plaintext在GetDirectoryEntryTest方法中,获取LDAP路径,比较预期与实际DirectoryEntry对象。

我有以下代码:

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

尝试将对象的路径与以下内容进行比较:

VB.NET如何编写高效的问题单元测试?

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.