Collection.Count shows zero even though Collection.GetEnumerator.Current contains a reference after MoveNext()

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật