2015-01-15 49 views
0

我在Visual Studio 2010中有一個測試項目。在Visual Studio測試項目中添加「互聯網關閉」測試

其中一個測試類應該發送一些互聯​​網請求。

我想編寫一個測試,如:

[TestMethod()] 
[ExpectedException(typeof(InternetDownException))] 
public void ThrowsInternetDownExceptionWhenNoInternet() 
{ 
    // How do I fake internet down?? 
    target.Connect(); // this is supposed to throw 
} 

明確指出,這不是一個嚴格的單元測試項目,因爲它訪問互聯網(以及在某些方法的文件系統)是非常重要的,但無論如何,我希望進行這些測試,在一個與我的其他經常運行的真正的單元測試項目分離的項目中。

回答

0

我最終成功地使用了下面的方法。請注意,我用的是「硬編碼」連接名稱(「登錄時間本地」在我的情況),但也有方法來查找活動連接的名稱(留給讀者; O)

public static void DisableInternet() 
    { 
     Process process = new Process(); 
     ProcessStartInfo psi = new ProcessStartInfo(
      "netsh", 
      "interface set interface name=\"Conexão local\" admin=disabled" 
     ); 
     process.StartInfo = psi; 
     process.Start(); 
    } 

    public static void EnableInternet() 
    { 
     Process process = new Process(); 
     ProcessStartInfo psi = new ProcessStartInfo(
      "netsh", 
      "interface set interface name=\"Conexão local\" admin=enabled" 
     ); 
     process.StartInfo = psi; 
     process.Start(); 
    } 

(也期待在它現在,很容易幹這些方法很多...)

相關問題