我在Visual Studio 2012中的单元测试中有以下代码.我正在尝试测试该GatherData
方法在ExcelFile
类中的私有方法.但是,当我运行测试时,我得到了一个MissingMethodException
.如何在类中调用私有方法以便进行单元测试?
ExcelFile xFile = new ExcelFile("pathhere"); PrivateObject po = new PrivateObject(xFile); var retVal = po.Invoke("GatherData");
这是一些ExcelFile类:
public class ExcelFile { private FileInfo excelFileInfo; private ExcelWorksheet workSheet; private Dictionarydata = new Dictionary (); public ExcelFile(string path) { this.excelFileInfo = new FileInfo(path); } private Dictionary GatherData(ExcelWorksheet workSheet) { Dictionary data = new Dictionary (); int endDataRow = workSheet.Dimension.Rows; for (int rowNumber = 2; rowNumber <= endDataRow; rowNumber++) { if (ValidateRow(rowNumber)) { string columnOneValue = workSheet.Cells[rowNumber, 1].Value.ToString().Trim(), columnTwoValue = workSheet.Cells[rowNumber, 2].Value.ToString().Trim(); data.Add(columnOneValue, columnTwoValue); } } return data; } }
小智.. 6
GatherData方法需要一个ExcelWorksheet类型的参数才能工作.如果您创建ExcelWorksheet的对象,然后使用代码:
ExcelFile xFile = new ExcelFile("pathhere"); PrivateObject po = new PrivateObject(xFile); var retVal = po.Invoke("GatherData", new object[] {excelWorksheetObject});
它应该适合你.
查看这篇文章了解更多详细信息:运行到System.MissingMethodException:找不到PrivateObject的方法
干杯!
GatherData方法需要一个ExcelWorksheet类型的参数才能工作.如果您创建ExcelWorksheet的对象,然后使用代码:
ExcelFile xFile = new ExcelFile("pathhere"); PrivateObject po = new PrivateObject(xFile); var retVal = po.Invoke("GatherData", new object[] {excelWorksheetObject});
它应该适合你.
查看这篇文章了解更多详细信息:运行到System.MissingMethodException:找不到PrivateObject的方法
干杯!