6

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

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

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

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 也有效

4

1 回答 1

4

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


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

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

现在更改存根工作表服务以保留实际数据的副本(而不仅仅是 a 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

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

在 Rubberduck 测试资源管理器中通过测试


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

于 2018-04-25T15:05:00.117 回答