Note: An answer in either VB.NET or C# will be fine. I have no preference for this Q&A.
I’m trying to test my networking code and I’m running into a very strange problem: the GatewayAddresses
collection on my mocked NetworkInterface
always returns a Count
of 0
, even though its Enumerator
‘s internal list clearly contains an item:
I inserted this debugging code just before the assertions, and it works as expected:
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
The string Gateway IP Address: 192.168.1.1
is written to the test results.
This assertion also succeeds:
Dim lMoved = oGatewayAddresses.GetEnumerator.MoveNext
oGatewayAddresses.GetEnumerator.Current.Address.Should.Be(oIPAddress)
But no matter what I try, the collection count always returns zero.
What could be the problem? Here’s my code:
Service
Public Interface INicService
Function GetActiveNetworkInterfaces() As IEnumerable(Of NetworkInterface)
End Interface
Test
<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