Assembly.GetEntryAssembly().CodeBase precludes unit testing
|May 24, 2012||Posted by forumadmin under .Net||
I’m writing a logging library. I want the library, by default, to write to a directory in the common application data folder named for the application. For example, if the application is called “MyApplication.exe”, I want the data saved in “C:\ProgramData\MyApplication”.
I’m using this code to construct the path:
private static string loggingDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().CodeBase) + Path.DirectorySeparatorChar;
This works exactly as expected, with one problem. I can’t unit test the library!
When I try to run the unit tests they all fail with a System.NullReferenceException. If I replace the “Assembly.GetEntryAssembly().CodeBase” call with a string the unit tests once again function properly.
I think I understand why this happens but I have no idea how to work around the problem. I hope someone will be able set me on the path of righteousness.
|Asked By – casterle||Read Answers|