我想使用此处描述的过程:Automated Testing OpenXML SDK
(也在此处触摸:Unit testing an application that talks to microsoft word via OpenXML)
但是,模拟这样的东西需要什么?
我做了以下界面:
public interface IExcelDocument
{
Row GetRow(uint rowIndex, SheetData sheetData);
SharedStringTablePart GetSharedStringTablePart(SpreadsheetDocument excelDoc);
WorksheetPart GetWorksheetPart(SpreadsheetDocument excelDoc, string sheetName);
Cell InsertCellInWorksheet(string columnName, uint rowIndex, WorksheetPart worksheetPart);
Row InsertRow(WorksheetPart worksheetPart);
int InsertSharedStringItem(string text, SharedStringTablePart shareStringPart);
}
我可以想象嘲笑看起来像这样:
[TestMethod()]
public void Excel_GetWorkseetPartTest()
{
Mock<IExcelDocument> mockExcelDocument = new Mock<IExcelDocument>();
string sheetName = "sheet";
var excelMock = mockExcelDocument.Object.GetWorksheetPart(MySpreadsheetDocument, sheetName);
Assert.IsTrue(excelMock != null);
}
我要进行单元测试的
GetWorksheetPart
方法驻留在实现接口IExcelDocument
的类中,如下所示:public WorksheetPart GetWorksheetPart(SpreadsheetDocument excelDoc, string sheetName)
{
Sheet sheet = excelDoc.WorkbookPart.Workbook.Descendants<Sheet>()
.SingleOrDefault(s => s.Name == sheetName);
if (sheet == null)
{
throw new ArgumentException(
String.Format("No sheet named {0} found in spreadsheet {1}",
sheetName, _filePath), "sheetName");
}
return (WorksheetPart)excelDoc.WorkbookPart.GetPartById(sheet.Id);
}
我无法环绕
MySpreadsheetDocument
,因为我还需要实现SpreadsheetDocument.Open
方法,即使这是合理的,也不确定。这是我所说的
GetWorksheetPart
:using (SpreadsheetDocument _excelDoc = SpreadsheetDocument.Open(_filePath, true))
{
IExcelDocument excelDoc = new ExcelDocument();
WorksheetPart worksheetPart = excelDoc.GetWorksheetPart(_excelDoc, sheetName);
}
最佳答案
您在为单元测试抽象化依赖项的概念感到困惑。
给出一个示例类
public class ExcelDocument {
public WorksheetPart GetWorksheetPart(SpreadsheetDocument excelDoc, string sheetName)
{
Sheet sheet = excelDoc.WorkbookPart.Workbook.Descendants<Sheet>()
.SingleOrDefault(s => s.Name == sheetName);
if (sheet == null)
{
throw new ArgumentException(
String.Format("No sheet named {0} found in spreadsheet {1}",
sheetName, _filePath), "sheetName");
}
return (WorksheetPart)excelDoc.WorkbookPart.GetPartById(sheet.Id);
}
}
此方法取决于外部组件
SpreadsheetDocument
在这种情况下,
SpreadsheetDocument
是需要抽象的。查看被测方法,该方法必须能够获得
Sheet
,因此您的抽象必须提供该功能。它还需要能够获得WorksheetPart
由此可以得出以下接口
public ISpreadsheetDocument {
Sheet GetSheet(string name);
WorksheetPart GetPartById(string id);
}
这将被测方法更改为此
public WorksheetPart GetWorksheetPart(ISpreadsheetDocument excelDoc, string sheetName)
{
Sheet sheet = excelDoc.GetSheet(sheetName);
if (sheet == null)
{
throw new ArgumentException(
String.Format("No sheet named {0} found in spreadsheet {1}",
sheetName, _filePath), "sheetName");
}
return excelDoc.GetPartById(sheet.Id);
}
现在,如果需要进行单元测试,则可以对excelDoc进行模拟/隔离,然后您的生产实现将包装外部功能。
public class SpreadsheetDocumentWrapper : ISpreadsheetDocument {
private SpreadsheetDocument excelDoc;
public SpreadsheetDocumentWrapper(SpreadsheetDocument excelDoc) {
this.excelDock = excelDock;
}
public Sheet GetSheet(string name) {
return excelDoc.WorkbookPart.Workbook.Descendants<Sheet>()
.SingleOrDefault(s => s.Name == sheetName);
}
public WorksheetPart GetPartById(string id) {
return (WorksheetPart)excelDoc.WorkbookPart.GetPartById(id);
}
}
因此,您需要做的是查看您的
ExcelDocument
类,确定其依赖项,并将这些依赖项抽象为可用于单元测试的服务。