Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:
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.
 
var s = Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetExecutingAssembly().Location).LocalPath)
 

Static method in class FolderSettings
public static String ProgramFolder
{
    get {
        var s = (Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location));
        return s;
    }
}
 
TestMethod:
 
/// <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 22-Feb-12 5:37am
Erik Rude4.2K
Edited 22-Feb-12 5:58am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Erik Rude at 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 at 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 at 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)

  Print Answers RSS
0 Maciej Los 295
1 OriginalGriff 259
2 Sergey Alexandrovich Kryukov 205
3 Aajmot Sk 187
4 Sinisa Hajnal 176
0 OriginalGriff 7,800
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 22 Feb 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100