注意:用 VB.NET 或 C# 回答都可以。我对这个问答没有偏好。
我正在尝试测试我的网络代码,但遇到了一个非常奇怪的问题:我模拟的
GatewayAddresses
上的 NetworkInterface
集合始终返回 Count
的 0
,即使它是 Enumerator
'的内部列表显然包含一个项目:
我在断言之前插入了此调试代码,它按预期工作:
Dim oEnumerator2 As IEnumerator(Of GatewayIPAddressInformation) = oGatewayAddresses.GetEnumerator
While oEnumerator2.MoveNext()
Dim oCurrentGatewayInfo As GatewayIPAddressInformation = oEnumerator2.Current
Me.Output.WriteLine($"Gateway IP Address: {oCurrentGatewayInfo.Address}")
End While
字符串
Gateway IP Address: 192.168.1.1
被写入测试结果。
这个断言也成功了:
Dim lMoved = oGatewayAddresses.GetEnumerator.MoveNext
oGatewayAddresses.GetEnumerator.Current.Address.Should.Be(oIPAddress)
但无论我如何尝试,集合计数始终返回零。
可能是什么问题?我的代码如下。
--编辑--
问题要么出在我的模拟设置中,要么出在 NSubstitute 本身。我更倾向于考虑前者。
在这个小小的修改下,测试通过了:
'oNicService = Substitute.For(Of INicService)
oNicService = New NicService
' The actual function:
' Public Function GetActiveNetworkInterfaces() As IEnumerable(Of NetworkInterface) Implements INicService.GetActiveNetworkInterfaces
' Return NetworkInterface.GetAllNetworkInterfaces.Where(Function(Nic) Nic.OperationalStatus = OperationalStatus.Up)
' End Function
' Act
oActiveNics = oNicService.GetActiveNetworkInterfaces
oGatewayAddresses = oActiveNics.First.GetIPProperties.GatewayAddresses
' Assert
oGatewayAddresses.Should.NotBeNull()
oGatewayAddresses.Should.HaveCount(1) ' <-- This succeeds
我应该如何调整我的模拟设置才能使其正常工作?
服务
Public Interface INicService
Function GetActiveNetworkInterfaces() As IEnumerable(Of NetworkInterface)
End Interface
测试
<Fact>
Public Sub GetActiveNetworkInterfaces_Should_Return_Correct_Gateway_Address()
' Arrange
Dim oMockGatewayAddresses As GatewayIPAddressInformationCollection
Dim oGatewayAddresses As GatewayIPAddressInformationCollection
Dim oMockGatewayInfo As GatewayIPAddressInformation
Dim oGatewayList As List(Of GatewayIPAddressInformation)
Dim oEnumerator As IEnumerator(Of GatewayIPAddressInformation)
Dim oActiveNics As IEnumerable(Of NetworkInterface)
Dim oNicService As INicService
Dim oIPAddress As IPAddress
Dim oMockNic1 As NetworkInterface
oMockGatewayAddresses = Substitute.For(Of GatewayIPAddressInformationCollection)
oMockGatewayInfo = Substitute.For(Of GatewayIPAddressInformation)
oIPAddress = IPAddress.Parse("192.168.1.1")
oMockNic1 = Substitute.For(Of NetworkInterface)
' Mock GatewayIPAddressInformation
oMockGatewayInfo.Address.Returns(oIPAddress)
oMockNic1.OperationalStatus.Returns(OperationalStatus.Up)
' Mock GatewayIPAddressInformationCollection
oGatewayList = New List(Of GatewayIPAddressInformation) From {oMockGatewayInfo}
' Create an enumerator that implements IEnumerator(Of GatewayIPAddressInformation)
oEnumerator = oGatewayList.GetEnumerator
' Configure the mock to return the enumerator
oMockGatewayAddresses.GetEnumerator.Returns(oEnumerator)
' Configure GetIPProperties.GatewayAddresses to return the mocked collection
oMockNic1.GetIPProperties.GatewayAddresses.Returns(oMockGatewayAddresses)
oNicService = Substitute.For(Of INicService)
oNicService.GetActiveNetworkInterfaces.Returns({oMockNic1})
' Act
oActiveNics = oNicService.GetActiveNetworkInterfaces
oGatewayAddresses = oActiveNics.First.GetIPProperties.GatewayAddresses
' Assert
oGatewayAddresses.Should.NotBeNull()
oGatewayAddresses.Should.HaveCount(1) ' <-- Fails here
oGatewayAddresses.First.Address.Should.Be(oIPAddress)
End Sub
ChatGPT 来救援。
好吧,事实证明我嘲笑了错误的事情,而不是嘲笑了正确的事情。
这是通过的更新测试:
<Fact>
Public Sub GetActiveNetworkInterfaces_Should_Return_Single_NetworkInterface_With_ExpectedGateway()
Dim oActiveNetworkInterfaces As IEnumerable(Of NetworkInterface)
Dim oSingleNetworkInterface As NetworkInterface
Dim oExpectedGatewayAddress As IPAddress
Dim oGatewayCollection As GatewayIPAddressInformationCollection
Dim oGatewayAddresses As List(Of IPAddress)
Dim oIpProperties As IPInterfaceProperties
Dim oGatewayList As List(Of GatewayIPAddressInformation)
Dim oGatewayInfo As GatewayIPAddressInformation
Dim oNicService As INicService
Dim oNics As List(Of NetworkInterface)
Dim oNic As NetworkInterface
' Arrange
oGatewayCollection = Substitute.For(Of GatewayIPAddressInformationCollection)
oIpProperties = Substitute.For(Of IPInterfaceProperties)
oGatewayInfo = Substitute.For(Of GatewayIPAddressInformation)
oNicService = Substitute.For(Of INicService)
oNic = Substitute.For(Of NetworkInterface)
oNic.Name.Returns("Ethernet")
oNic.OperationalStatus.Returns(OperationalStatus.Up)
oExpectedGatewayAddress = IPAddress.Parse("192.168.1.254")
oGatewayInfo.Address.Returns(oExpectedGatewayAddress)
oGatewayList = New List(Of GatewayIPAddressInformation) From {oGatewayInfo}
oGatewayCollection.GetEnumerator.Returns(oGatewayList.GetEnumerator)
oGatewayCollection.Count.Returns(oGatewayList.Count)
oIpProperties.GatewayAddresses.Returns(oGatewayCollection)
oNic.GetIPProperties.Returns(oIpProperties)
oNics = New List(Of NetworkInterface) From {oNic}
oNicService.GetActiveNetworkInterfaces.Returns(oNics)
' Act
oActiveNetworkInterfaces = oNicService.GetActiveNetworkInterfaces
' Assert
oActiveNetworkInterfaces.Should.ContainSingle()
oSingleNetworkInterface = oActiveNetworkInterfaces.Single
oGatewayAddresses = oSingleNetworkInterface.
GetIPProperties.
GatewayAddresses.
Select(Function(Gateway)
Return Gateway.Address
End Function).ToList
oGatewayAddresses.Should.ContainSingle()
oGatewayAddresses.Single.Should.Be(oExpectedGatewayAddress)
End Sub