views:

235

answers:

3

How to handle the Network failure error message,and how to simulate it?

+2  A: 

The way to simulate a network failure (or any sub-system failure) is to replace the real network with a mock object. Then in your mock network object you can set up the method calls to return what ever you require for your testing purposes, be it a successful connection or a failure at any point along the way.

Your code can then be tested against this to ensure that it gracefully fails and reports sensible error messages.

If you look into mocking frameworks you'll get a lot more information.

ChrisF
By any chance do u know which class library is used to check the network is active,suppose i disable the connection,and i wanna check if its active or not , that can be taken as network failure right?
+1  A: 

Do you mean you want the boolean expression

 My.Computer.Network.IsAvailable

You can then just set up some kind of GUI thread to periodically check its value.

stacked
+1  A: 

While I think ChrisF has the better solution, in the past I've usually only cared whether the network is "up" or not. So my test is a bit more simple. I yank the ethernet cable out while the app is running and see if my app responds as I expect.

Josh W.