gpt4 book ai didi

vba - 如何对 VBA 代码进行单元测试? - 两个不同的指针

转载 作者:行者123 更新时间:2023-12-04 20:29:12 27 4
gpt4 key购买 nike

我正在处理 this优秀的教程,但最终我的第一个测试没有通过,因为我可以清楚地看到我正在创建两个不同的数组(和指针),并试图将它们相互比较。

现在,我所看到的教程遗漏了我添加的几行代码,这也是我看到问题的地方,但没有这些行,代码甚至不会运行粗糙。

我的所有其他测试方法都与示例相同,除了我创建了以下几行的方法 - 否则在运行测试时不会发生任何事情。

Public Sub Run(ByVal dataService As IDataService, ByVal wsService As IWorksheetService)
Dim data As Variant 'Added this line
data = dataService.GetSomeTable 'Added this line
Call wsService.WriteAllData(data) 'Added this line
End Sub

在这里我可以看到代码向南......
'@TestMethod
Public Sub WorksheetServiceWorksOffDataFromDataService()
'Arrange
Dim dataServiceStub As MyDataServiceStub
Set dataServiceStub = New MyDataServiceStub
Dim expected As LongLong
expected = VarPtr(dataServiceStub.GetSomeTable) 'expected creates an Array

Dim wsServiceStub As MyWorksheetServiceStub
Set wsServiceStub = New MyWorksheetServiceStub

'Act
With New MyTestableMacro
.Run dataServiceStub, wsServiceStub 'here we create a second array
End With

Dim actual As LongLong
actual = wsServiceStub.WrittenArrayPointer 'here we point to the address of the second array

'Assert
Assert.AreEqual expected, actual ' this test fails cause it points to two different addresses
End Sub

由于 64 位上的数字对于 Long 来说太长了,我不得不将数组指针的教程中的类型从 Long 更改为 LongLong。 LongPtr 也有效

最佳答案

VarPtr是什么人为地使该测试复杂化,引入了不需要存在的脆弱和有缺陷的指针逻辑。

更改 stub 数据服务以返回一些非空数据 - 实际上任何事情都可以:

Option Explicit
Implements IDataService
'@Folder "Services.Stubs"

Private Function IDataService_GetSomeTable() As Variant
IDataService_GetSomeTable = GetSomeTable
End Function

Public Function GetSomeTable() As Variant
Dim result(1 To 50, 1 To 10) As Variant
result(1, 1) = "test"
GetSomeTable = result
End Function

现在更改 stub 工作表服务以保留实际数据的副本(而不仅仅是 LongPtr ):
Option Explicit
Implements IWorksheetService
'@Folder "Services.Stubs"

Private Type TStub
WasWritten As Boolean
WrittenData As Variant
End Type
Private this As TStub

Private Sub IWorksheetService_WriteAllData(ByRef data As Variant)
this.WasWritten = True
this.WrittenData = data
End Sub

Public Property Get DataWasWritten() As Boolean
DataWasWritten = this.WasWritten
End Property

Public Property Get WrittenData() As Variant
WrittenData = this.WrittenData
End Property

现在更改测试以断言 IDataService.GetSomeTable返回与 IWorksheetService.WriteAllData 相同的数据使用 - 您可以使用 Assert.SequenceEquals 来做到这一点,它比较两个数组的所有元素,如果有任何不同则失败:
'@TestMethod
Public Sub WorksheetServiceWorksOffDataFromDataService()
'Arrange
Dim dataServiceStub As StubDataService
Set dataServiceStub = New StubDataService
Dim expected As Variant
expected = dataServiceStub.GetSomeTable

Dim wsServiceStub As StubWorksheetService
Set wsServiceStub = New StubWorksheetService

'Act
With New Macro
.Run dataServiceStub, wsServiceStub
End With

Dim actual As Variant
actual = wsServiceStub.WrittenData

'Assert
Assert.SequenceEquals expected, actual
End Sub

这使得测试更加简单,并且它通过了:

Passing tests in Rubberduck test explorer

我将在今天晚些时候用这个更简单的测试更新这篇文章。

关于vba - 如何对 VBA 代码进行单元测试? - 两个不同的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50023782/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com