我有單元測試具有此方法的類的方法如何使用靜態類,沒有Moles或Isolator的單元測試方法?
public int GetHighestPriorityPriceRecordIndex(SearchAndExtractReply_2 priceInfoReply)
{
int index = 0;
var priceExposableColumn = GetPriceColumn(priceInfoReply, "LPCIsPriceExposable");
var pricingSourceIDColumn = GetPriceColumn(priceInfoReply, "LPCPricingSourceID");
var priceExposablePriority = new Dictionary<string, int> { { "Y", 2 }, { "N", 1 } };
var pricingSourcePriority = new Dictionary<string, int> { { "USA", 5 }, { "EME", 4 }, { "ASI", 3 }, { "DER", 2 }, { "NUS", 1 } };
if (priceExposableColumn.Value != null)
for (int i = 1; i < priceExposableColumn.Value.Length; i++)
if (priceExposablePriority[priceExposableColumn.Value[i]] > priceExposablePriority[priceExposableColumn.Value[index]]
&& pricingSourcePriority[pricingSourceIDColumn.Value[i]] > pricingSourcePriority[pricingSourceIDColumn.Value[index]])
index = i;
return index;
}
private static StringColumn GetPriceColumn(SearchAndExtractReply_2 priceInfoReply, string columnName)
{
return (StringColumn)SearchAndExtractReply_2_Extension.GetColumn(priceInfoReply, columnName);
}
我想,這種方法使用靜態類和方法 SearchAndExtractReply_2_Extension.GetColumn。 如何更好地重構此代碼,以便我可以在沒有Moles或Isolator的情況下測試此方法?或者更好地使用痣來隔離代碼? 在此先感謝。
由於我udnderstood我需要你的依賴注入正確嗎? – Serghei 2012-01-31 16:16:29
是的,這將有助於使代碼更加清晰,並且會爲您提供接縫,您可以插入模擬以確保可以獨立測試代碼。 – 2012-01-31 16:22:17
問題是我在一個大的遺留解決方案中工作,並且包含IoC容器需要重構大量代碼和噸代碼。 – Serghei 2012-01-31 16:29:41