2

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..

4

3 回答 3

2

是否正在LINQDataAccess.EICDataClassesDataContext寻找 web.config 或其他一些外部数据源进行设置?

我可以告诉您一个事实,即您必须跳过障碍才能web.config访问您的测试代码。

更新 啊,是的。我看到您在ConfigurationManager测试失败的线路上使用... ConfigurationManager查看 web.config 进行配置。当我编写测试时,这一直是我的症结所在。

您需要更改代码以便可以在没有 web.config 的情况下实例化该类,或者您需要制作它以便您的测试可以访问 web.config。

于 2010-05-25T14:54:30.833 回答
2

您的测试项目是否有自己的配置文件?这种行为通常意味着应用找不到连接字符串。测试项目需要自己的文件,因为它们不在客户端应用程序的上下文中运行。

更新您在添加 app.config 后描述的错误在测试基于 SQLExpress 构建的 Web 应用程序并附加 .mdf 时很常见。SQLExpress 一次不能在多个进程中运行。因此,如果您之前运行过您的 Web 应用程序,它可能仍然处于活动状态,并且会与附加数据库的尝试发生冲突。

您可以使用 SQL Management Studio 永久附加数据库,然后使用更传统的连接字符串,例如:

Server=myServer;Database=EICDatabase;Trusted_Connection=True;
于 2010-05-25T14:55:53.617 回答
0

对我来说,您的问题似乎是未设置的连接字符串。我假设您的单元测试与 DAL 位于不同的项目中。您在没有连接字符串的情况下调用 datacontext 构造函数的“new”命令。所以它通常应该在设置时使用它的默认值。但是由于此设置通常存储在另一个项目的 web.config 中,因此没有设置连接字符串,您会收到错误消息。

如果它是正确的,我假设你必须将 DAL 项目中的设置放入单元测试项目中。最简单的解决方案应该是将 web.config 连接字符串复制到单元测试项目的 app.config 中。

当然还有其他可能性,但我认为将 web.config 配置放入您的单元测试项目并不容易。

于 2010-05-25T14:53:29.517 回答