I am having unit testing classes that use EPPlus. In my opinion, I have two options.
I can mock and paste the HttpPostedFileBase into the method, or I can mock and introduce the EPPlus ExcelPackage class.
Failing HttpPostedFileBase, at least makes the real layout seem limited. I can make fun of the basic properties in a file (MIME type, file name, etc.), but mocking its InputStream so that the tests really interact with it seems extremely complicated. The only solution I can come up with is to provide a real excel file, create a real FileStream with it, and assign this FileStream to my HttpPostedFileBase InputStream layout. But then this is technically an integration test, not a unit test.
const string FakeFileName = "TestExcelFile.xlsx";
var fileStream = new FileStream(FilePath, FileMode.Open);
var fakeFile = A.Fake<HttpPostedFileBase>();
A.CallTo(() => fakeFile.InputStream).Returns(fileStream);
I decided if I want to do the actual unit test, I could make fun of and introduce the EPPlus ExcelPackage class. Then I could make fun of the associated Worksheet, Columns and Cell classes by dynamically setting their properties according to the conditions of my test, without touching the real file. The problem is that most EPPlus classes are sealed, so I can't mimic them using FakeItEasy. I tried to create wrapper classes for them (see below), so I could make fun of the wrapper class instead ... but some of the classes that I need for mock / wrap have internal constructors, so I cannot create them. (I tried to work around the problem of the internal constructor using a couple of ugly hacks, but was unsuccessful.) And so I hit the wall with this option.
. , - , . , , , excel ? , .
public class ExcelWorksheetsWrapper : IEnumerable<ExcelWorksheet>
{
public readonly ExcelWorksheets _excelWorksheets;
public ExcelWorksheetsWrapper()
{
_excelWorksheets = new ExcelWorksheets();
}
public ExcelWorksheet Add(string worksheetName)
{
return _excelWorksheets.Add(worksheetName);
}
public IEnumerator<ExcelWorksheet> GetEnumerator()
{
return _excelWorksheets.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _excelWorksheets.GetEnumerator();
}
}