Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm a bit new to unit testing, most of my tests are fairly easy to do and just works brilliantly. However I'm trying to test this simple function and it fails me with the following error:
XML
System.NullReferenceException: Object reference not set to an instance of an object.

What am I doing wrong? VS2010SP1
SOLUTION: Thanks to the solution below I realized my mistakes and changed one line to this and now all is bliss.

XML
var s = Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetExecutingAssembly().Location).LocalPath)



Static method in class FolderSettings
XML
public static String ProgramFolder
{
    get {
        var s = (Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location));
        return s;
    }
}


TestMethod:

XML
/// <summary>
///A test for ProgramFolder
///</summary>
[TestMethod()]
public void ProgramFolderTest()
{
    string actual;
    actual = FolderSettings.ProgramFolder;
    Assert.Inconclusive("Verify the correctness of this test method.");
}
Posted
Updated 22-Feb-12 4:58am
v2

1 solution

When you're running the unit tests, the entry assembly will be the test hosting shell. That could be something useless like the nunit.exe assembly, or if you're using a binary test harness (e.g. Visual Studio itself), it will be null. (See the documentation for GetEntryAssembly[^].)

You should either use GetExecutingAssembly, if that's what you actually want to know in the general case (i.e. if someone links to your assembly, do you want the location of your assembly still?), or accept that something like this which is making assumptions about environment (i.e. that a particular assembly will be the primary entry point) is intrinsically not unit testable and not worry about it.

Considering it's a one line call to an external dependency (the Framework) which you can assume is well tested, I don't think you need a (pseudo-)unit test for this property anyway.
 
Share this answer
 
Comments
Erik Rude 22-Feb-12 10:50am    
First of all Thanks!
OK, the thing is that I am of course using this feature in other 'units' that fail because I don't get the right path (that would be a null as you said). I had been looking at the GetExecutingAssembly, but abandoned it because I'm lazy and it gave me uripaths. I would assume in my program that the Executing assembly path was always the one I'm after.
BobJanova 22-Feb-12 11:06am    
If you're relying on an external dependency like this for other tests, then they're not really unit tests in the strictest sense. See if you can make everything genuinely unit testable, and pass in a mocked-out path instead of actually asking the environment.

Some things (file access, database connection code etc) actually needs integration testing (i.e. needs to know stuff like this), but it's a good discipline to carefully isolate that and to provide carefully controlled mocks to unit tests of logic.
Erik Rude 23-Feb-12 3:58am    
Thanks, it looks like I should do a bit of reading up on the theoretical background of Unit Testing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900