So I’ve been working through some issues, and I’m real close, but I’m running into a problem.
So here’s how I call my factory:
ClassImporter classImporter = new ClassImporter(importOptions);
There are multiple types of ClassImporter objects, here’s the base ClassImporter class with one of the types.
public class ClassImporter
{
public ImportOptions Options;
public ClassImporter Importer;
public ClassImporter()
{
}
public ClassImporter(ImportOptions options)
{
this.Options = options;
this.Importer = ClassImporterFactory.GetImporter(options);
}
public virtual List<Class> Import()
{
return Importer.Import();
}
}
public class ExcelImporter : ClassImporter, IClassImporter
{
public ExcelImporter() : base()
{
}
public override List<Class> Import()
{
if (base.Options.FileLocation == string.Empty)
{
throw new BlankFilenameException("A blank Excel file location was supplied.");
}
return new List<Class>();
}
}
And here’s that Factory class you see in there:
public class ClassImporterFactory
{
public static ClassImporter GetImporter(ImportOptions options)
{
switch (options.FileType)
{
case FileType.CSV:
return new CSVImporter(options);
case FileType.Excel:
return new ExcelImporter();
case FileType.MySQL:
return new MySQLImporter(options);
case FileType.Oracle:
return new OracleImporter(options);
case FileType.ScreenScraper:
return new ScreenScraperImporter(options);
case FileType.SQL:
return new SQLImporter(options);
case FileType.XML:
return new XMLImporter(options);
case FileType.NotSet:
default:
throw new BlankImportTypeException("Import type was not specified in the Import Options.");
}
}
}
Now I’m trying to Assert on that Exception you see in the ExcelImporter class, with this line:
classImporter.Import();
My test is attributed to have an ExpectedException, but it looks like when I debug through it, that the Importer hosted on the ClassImporter class is basically looking at it’s own options instead of the base options of the main class that created it, and then therefore doesn’t see the options passed in, just a null copy of them.
How can I redo this?
EDIT: Full Test:
[TestMethod]
[ExpectedException(typeof(BlankFilenameException), "A blank Excel file location was supplied.")]
public void LoadExcelFile_EmptyName_ReturnsBlankFilenameException()
{
ImportOptions importOptions = new ImportOptions(FileType.Excel, string.Empty);
ClassImporter classImporter = new ClassImporter(importOptions);
classImporter.Import();
}
ExcelImporter’s the only one of your classes that you’re using the default constructor for. So it never sets the Options field. The parameterless base constructor doesn’t set it either.
So, basically, you make a ClassImporter instance with the right options, and set its Importer property to a new ExcelImporter instance with no options.
You can fix this in a few ways, but the easiest way is to make ExcelImporter work the same as your other subclasses and have a constructor that takes ImportOptions. Then call that constructor from GetImporter. Don’t chain them though – that’ll loop forever.
Really, I don’t understand why ExcelImporter’s a derived class of ClassImporter. It doesn’t use any of ClassImporter’s functionality, and you’re setting yourself up for an infinite loop with constructors. Just give it the options when you make the instance.