I have a WPF window which simply displays statis data, along with the computer's active IPv4 addresses. When constructed, the ViewModel calls the following code (in a separate thread):
private void GetIpAddresses()
{
var resultList = new List<string>();
if (NetworkInterface.GetIsNetworkAvailable() != false)
{
foreach (var item in NetworkInterface.GetAllNetworkInterfaces())
{
if (item.NetworkInterfaceType != NetworkInterfaceType.Loopback &&
item.OperationalStatus == OperationalStatus.Up)
{
resultList.AddRange(from address in item.GetIPProperties().UnicastAddresses
where address.Address.AddressFamily == AddressFamily.InterNetwork
select address.Address.ToString());
}
}
}
IpAddresses = FormatIpAddresses(resultList);
}
IpAddresses is bound to a control in the view, and that's it.
I'm wondering if I should unit test this code. This is more of an integration test, but even then I wouldn't know how to fake having/not having an active IPv4 address.
But if I do want to unit test this, how would I mock the static method calls to the .Net framework (e.g. NetworkInterface.GetIsNetworkAvailable())?
I could create my own wrapper class that simply calls the static methods, extract an interface and create a mock. But if I take that strategy further into the rest of my application, I'll be creating quite a few redundant wrapper around the .Net framework.
I could also have the method GetIpAddresses take in two delegates, one that returns the answer to "is network available" and another one that returns all network interfaces. But then the real caller of this method would have to practically pass GetIpAddresses half of its logic, and that breaks the whole idea of separation of concerns... doesn't it?