Alex
Alex

Reputation: 451

Problem with Unit testing of ASP.NET project (NullReferenceException when running the test)

I'm trying to create a bunch of MS visual studio unit tests for my n-tiered web app but for some reason I can't run those tests and I get the following error -

"Object reference not set to an instance of an object"

What I'm trying to do is testing of my data access layer where I use LINQ data context class to execute a certain function and return a result,however during the debugging process I found out that all the tests fail as soon as they get to the LINQ data context class and it has something to do with the connection string but I cant figure out what is the problem.

The debugging of tests fails here(the second line):

public EICDataClassesDataContext() :
    base(global::System.Configuration.ConfigurationManager.ConnectionStrings["EICDatabaseConnectionString"].ConnectionString, mappingSource)
    {
        OnCreated();
    }

And my test is as follows:

TestMethod()]
    public void OnGetCustomerIDTest()
    {
        FrontLineStaffDataAccess target = new FrontLineStaffDataAccess(); // TODO: Initialize to an appropriate value
        string regNo = "jonh"; // TODO: Initialize to an appropriate value
        int expected = 10; // TODO: Initialize to an appropriate value
        int actual;
        actual = target.OnGetCustomerID(regNo);
        Assert.AreEqual(expected, actual);
    }

The method which I call from DAL is:

 public int OnGetCustomerID(string regNo)
    {
        using (LINQDataAccess.EICDataClassesDataContext dataContext = new LINQDataAccess.EICDataClassesDataContext())
        {
            IEnumerable<LINQDataAccess.GetCustomerIDResult> sProcCustomerIDResult = dataContext.GetCustomerID(regNo);
            int customerID = sProcCustomerIDResult.First().CustomerID;

            return customerID;
        }
    }

So basically everything fails after it reaches the 1st line of DA layer method and when it tries to instantiate the LINQ data access class...

I've spent around 10 hours trying to troubleshoot the problem but no result...I would really appreciate any help!

UPDATE: Finally I've fixed this!!!!:) I dont know why but for some reasons in the app.config file the connection to my database was as follows:

AttachDbFilename=|DataDirectory|\EICDatabase.MDF

So what I did is I just changed the path and instead of |DataDirectory| I put the actual path where my MDF file sits,i.e

C:\Users\1\Documents\Visual Studio 2008\Projects\EICWebSystem\EICWebSystem\App_Data\EICDatabase.mdf

After I had done that it worked out!But still it's a bit not clear what was the problem...probably incorrect path to the database?My web.config of ASP.NET project contains the |DataDirectory|\EICDatabase.MDF path though..

Upvotes: 2

Views: 1627

Answers (3)

Gary.Ray
Gary.Ray

Reputation: 6501

Does your test project have it's own configuration file? This type of behavior usually means the app can't find the connection string. Test projects require their own file since they are not running in the context of the client app.

UPDATE The error you describe after adding an app.config is common when testing web applications built on SQLExpress and attaching an .mdf. SQLExpress cannot be run in more than one process at a time. So if you have previously run your web application it may still be active and will conflict with the attempt to attach the database.

You can use SQL Management Studio to attach the database permanently and then use a more traditional connection string like:

Server=myServer;Database=EICDatabase;Trusted_Connection=True;

Upvotes: 2

Vivian River
Vivian River

Reputation: 32400

Is LINQDataAccess.EICDataClassesDataContext looking to the web.config or some other outside source of data for its setup?

I can tell you for a fact that you must jump thru hoops to get web.config accessible to your test code.

Update Ah, yes. I see that you're using ConfigurationManager on the line where your test fails... ConfigurationManager looks to web.config for configuration. This has been a sticking point for me when I write my tests.

You need to either change the code so that the class can be instantiated without web.config, or you need to make it so that your tests can access web.config.

Upvotes: 2

Marks
Marks

Reputation: 3663

For me it seems like your problem is the connection string, which is not set. I assume your unit test is in a different project than the DAL. You call the 'new' command on the datacontext constructor without a connection string. So it should usually use its default, when set. But since this setting normally is stored in the web.config of the other project there is no connection string set and you get the error.

If its right, what i assume, you have to get the settings from the DAL project into the unit-testing project. Simplest solution should be to copy web.config connection string to app.config of unit test project.

There are other possibilities for sure, but i think its not easy to get the web.config configuration into your unit-testing project.

Upvotes: 0

Related Questions