Reputation: 115
I am using NUnit to test one functionality where I need to load XML file to object. The XML file is in location of the Console Application.
I have Following method where configuration will be read :
public string GetConfiguration(TempFlexProcessor processor)
{
var exePath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
var configPath = Path.Combine(Path.GetFullPath(exePath), "configuration");
var configFile = string.Format(@"{0}.xml", processor.GetType().Name);
}
Now in my NUnit Test I have test method where I test GetConfiguration :
[Test]
public void TempFlexProcessorExecuteTest()
{
#region Given
#endregion
#region When
var tempFlexProcessor = new TempFlexProcessor();
var actual = tempFlexProcessor.GetConfiguration(tempFlexProcessor);
#endregion
Assert.AreEqual("path of the file", actual);
}
But System.Reflection.Assembly.GetEntryAssembly() is null, please help.
Upvotes: 2
Views: 2666
Reputation: 115
I used AppDomain.CurrentDomain.BaseDirectory
instead of System.Reflection.Assembly.GetEntryAssembly().Location
Upvotes: 2
Reputation: 1502726
I suspect the problem is that NUnit is running your tests in a different AppDomain, but without using ExecuteAssembly
. From the documentation for Assembly.GetEntryAssembly
:
Gets the process executable in the default application domain. In other application domains, this is the first executable that was executed by
AppDomain.ExecuteAssembly
.
It's not clear which assembly you really want to get - even if this did return something "appropriate" for NUnit, that's likely to be the NUnit executable, which would be well away from any configuration directories you happen to have.
Basically, I think that you should at least provide an alternative way of specifying the configuration directory - and you might want to reconsider whether using GetEntryAssembly
is a good idea anyway. (Aside from anything else, it's slightly odd that you're calling GetConfiguration
on a processor and passing in another processor... that may be suitable for your design, but it's at least somewhat unusual, given that in your test case you're passing in a reference to the same object.)
Upvotes: 1