我有以下方法:正確的方式進行單元測試私有變量
private string _google = @"http://www.google.com";
public ConnectionStatus CheckCommunicationLink()
{
//Test if we can get to Google (A happy website that should always be there).
Uri googleURI = new Uri(_google);
if (!IsUrlReachable(googleURI, mGoogleTestString))
{
//The internet is not reachable. No connection is available.
return ConnectionStatus.NotConnected;
}
return ConnectionStatus.Connected;
}
的問題是,我怎麼得到它不嘗試谷歌(從而避免在互聯網上成爲了依賴)的連接。
最簡單的方法是取_google
並將其更改爲指向機器的本地位置。但要做到這一點,我需要製作_google
public
。我寧願不這樣做,因爲_google
不應該由應用程序改變。
我可以使`_google'成爲重載版本的方法(或對象構造函數)的參數。但是這也暴露了一個我從不希望應用程序使用的界面。其他選項是_google
internal
。但對於應用程序,internal
與public
相同。所以,其他人看不到_google
,應用界面仍然暴露它。
有沒有更好的方法?如果是這樣,請說明。
(另外,請不要在我的例子中挑選,除非它確實有助於找出一個解決辦法。我要求在一般情況下是這樣,不一定這個確切的例子想法。)
你想測試變量,還是CheckCommunicationLink你的測試用例? – Eiko 2010-09-07 19:48:46
@Eiko - 方法('CheckCommunicationLink') – Vaccano 2010-09-07 20:43:01
螞蟻這個測試應該測試*究竟是什麼*?對不起,我沒有得到這個測試的意圖。這種方法似乎只能檢查互聯網是否啓動。如果你僞造這些東西,你會得到什麼信息?爲什麼麻煩測試呢? – Eiko 2010-09-07 22:26:15