我有单元测试具有此方法的类的方法如何使用静态类,没有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