How to simulate "No Internet connection" in Unit / Integration test in C #

I have a component that (partially) uses an internet connection. I wrote several UnitTests to make sure the component is working. However, I would like to test the behavior of the component without internet connections.

So, my goal is to somehow temporarily disconnect the Internet or the entire Internet connection and reactivate after the test.

+4
source share
3 answers

There are many ways that a system can have “No Internet,” and the answer really depends on what you mean.

, . , , , .

, , ,

  1. DNS-, . .

, IP, , " ".

+1

\enable -

[ClassInitialize]
SelectQuery wmiQuery = new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionId != NULL");
ManagementObjectSearcher searchProcedure = new ManagementObjectSearcher(wmiQuery);
foreach (ManagementObject item in searchProcedure.Get())
{
    if (((string)item["NetConnectionId"]) == "Local Network Connection")
    {
       item.InvokeMethod("Disable", null);
    }
}

 [ClassCleanup()]
 // Enable local area connetcion
+3

Although the direct answer to your question is not correct, I believe that you can use it in this tool - https://jagt.imtqy.com/clumsy/download

I used it to work to simulate various network conditions for the mobile application that I am currently working on. You can completely disable the network connection by setting the packet drop to 100%.

+1
source

Source: https://habr.com/ru/post/1650077/


All Articles