位A NUnit的/ nMock /單元測試noobie問題:與NUnit的單元測試和nMocks
我試圖單元測試這個類。
我已經創建了一個模擬器,因爲我想知道從「getCurrencyRates」返回的值,以便可以基於該數據創建測試。
所以我創建了這個對象的模擬(只是爲了能夠知道返回的匯率)。
...但現在我也想調用這個類的其他一些方法。
我應該:
一)以某種方式實現從模擬對象的實際方法(甚至不知道這是可能的) B)重構,以便只有Web服務調用是在它自己的對象,並創建一個模擬那個 c)別的東西?
public class CurrencyConversion : ICurrencyConversion
{
public decimal convertCurrency(string fromCurrency, string toCurrency, decimal amount)
{
CurrencyRateResponse rates = getCurrencyRates();
var fromRate = getRate(rates, fromCurrency);
var toRate = getRate(rates, toCurrency);
decimal toCurrencyAmount = toRate/fromRate * amount;
return toCurrencyAmount;
}
public int addNumbers(int i, int j)
{
return i + j;
}
public decimal getRate(CurrencyRateResponse rates, string fromCurrency)
{
if (rates.rates.ContainsKey(fromCurrency))
{
return rates.rates[fromCurrency];
}
else
{
return 0;
}
}
public CurrencyRateResponse getCurrencyRates()
{
HttpWebRequest webRequest = GetWebRequest("http://openexchangeerates.org/latest.json");
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
string jsonResponse = string.Empty;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
jsonResponse = sr.ReadToEnd();
}
var serializer = new JavaScriptSerializer();
CurrencyRateResponse rateResponse = serializer.Deserialize<CurrencyRateResponse>(jsonResponse);
return rateResponse;
}
public HttpWebRequest GetWebRequest(string formattedUri)
{
// Create the request’s URI.
Uri serviceUri = new Uri(formattedUri, UriKind.Absolute);
// Return the HttpWebRequest.
return (HttpWebRequest)System.Net.WebRequest.Create(serviceUri);
}
}
我選擇nMock是因爲它在我正在使用的nUnit書中提到過。有什麼限制?我發現你只能模擬一個接口 - http://stackoverflow.com/questions/131806/mocking-classes-that-arent-interfaces – iKode 2012-02-15 08:49:50