c# - How to unit test method which uses static classes, without Moles or Isolator? -



c# - How to unit test method which uses static classes, without Moles or Isolator? -

i have method

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) (int = 1; < priceexposablecolumn.value.length; i++) if (priceexposablepriority[priceexposablecolumn.value[i]] > priceexposablepriority[priceexposablecolumn.value[index]] && pricingsourcepriority[pricingsourceidcolumn.value[i]] > pricingsourcepriority[pricingsourceidcolumn.value[index]]) index = i; homecoming index; } private static stringcolumn getpricecolumn(searchandextractreply_2 priceinforeply, string columnname) { homecoming (stringcolumn)searchandextractreply_2_extension.getcolumn(priceinforeply, columnname); }

i want unit test class has method, method uses static classes , method searchandextractreply_2_extension.getcolumn. how improve way refactor code in way can test method without moles or isolator? or improve utilize moles isolate code? in advance.

go refactoring if can. bringing moles in sounds bit of overkill, considering still have options create little code change.

and refactoring. introduce new class sole responsibility extract column searchandextractreply_2 object. it's simple:

public interface icolumnextractor { stringcolumn getpricecolumn(searchandextractreply_2 source, string columnname); } public class columnextractor : icolumnextractor { public stringcolumn getpricecolumn(searchandextractreply_2 source, string columnname) { homecoming (stringcolumn)searchandextractreply_2_extension.getcolumn(source, columnname); } }

now, either provide such class instance original method (adding new parameter of type icolumnextractor), or provide same extractor entire class (for illustration via constructor injection).

public int gethighestprioritypricerecordindex( searchandextractreply_2 priceinforeply, icolumnextractor columnextractor) { // utilize extractor object instead of static method }

then you're go free mocking framework (like fakeiteasy or moq).

c# unit-testing refactoring

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -