你不应该模拟你不拥有的类/对象。在这种情况下,因为您将方法耦合到CellSet
您现在直接依赖于它。
命名空间中的大多数类Microsoft.AnalysisServices.AdomdClient
都是密封的,并且不提供公共构造函数,这使得它们很难模拟/伪造。
查看CellSet
课程并确定您希望从中获得什么功能。提取您需要的属性/方法,并决定您想要在您可以控制的服务背后抽象什么。
这是我刚刚解释的简化示例。
public class MyClassUnderTest {
public DataTable ConvertCellSetToDataTable(ICellSetWrapper cellSet) {
if (cellSet == null) {
return null;
}
var dataTable = new DataTable();
SetColumns(cellSet, dataTable);
WriteValues(cellSet, dataTable);
return dataTable;
}
private void WriteValues(ICellSetWrapper cellSet, DataTable dataTable) {
//...assign value to datarows
}
private void SetColumns(ICellSetWrapper cellSet, DataTable dataTable) {
//...read data from this CellSet and build data columns
}
}
public interface ICellSetWrapper {
//...Methods and propeties exposing what you want to use
}
public class MyCellSetWrapper : ICellSetWrapper {
CellSet cellSet;
public MyCellSetWrapper(CellSet cellSet) {
this.cellSet = cellSet;
}
//...Implemented methods/properties
}
然后,您可以模拟所需的功能,以便使用您选择的测试框架测试您的方法。