我有超過十年的舊代碼,我必須寫單元測試以下邏輯。這是一個具體的類,下面的邏輯在於ctor。有沒有一種好的方法來編寫這種遺留代碼的單元測試/模擬。我正在使用MSTest/RhinoMocks框架和VS 2010 IDE與.Net框架4.0C# - 單元測試/嘲弄 - 遺留代碼
public class SomeClass
{
/// ctor
public SomeClass(XmlNode node)
{
//Step 1: Initialise some private variable based on attributes values from the node
//Step 2: Lot of If , else -if statements ---> something like -
if (/*attributeValue is something*/)
{
// Connect to Db, fetch some value based on the attribute value.
// Again the logic of connecting and fetching is in another concrete class
}
else if (/*attributeValue is somthing else*/)
{
// fetch a value by loading a config file (this loading and reading of config file
// is again a singleton class where config file path is hardcoded)
}
else
{
// set some private member variable
}
}
}
如果您無法重構可測試性,您可以查看類似於TypeMock Isolator的產品。它會花費你$ $$$,但... –