ASP.NET MVC unit tests with NUnit

asp.net-mvcnunit

I've been trying to learn ASP.NET MVC using the videos posted on the ASP.NET website and I'm running into a problem doing unit testing.

I have a very simple controller that uses LINQ to SQL to get an array of objects:

    public ActionResult Index()
    {
        ViewData["Message"] = "Welcome to ASP.NET MVC!";
        using (TrainingLogDataContext dc = new TrainingLogDataContext())
        {
            ViewData.Model = dc.Workouts.ToArray();
        }

        return View();
    }

This fails in NUnit with the following error:

at TrainingLog.Models.TrainingLogDataContext..ctor() in C:\Webs\TrainingLog\TrainingLog\Models\TrainingLog.designer.cs:line 41
at TrainingLog.Controllers.HomeController.Index() in C:\Webs\TrainingLog\TrainingLog\Controllers\HomeController.cs:line 16
at TrainingLogTests.Controllers.HomeControllerTest.Index() in C:\Webs\TrainingLog\TrainingLog.Tests\Controllers\HomeControllerTest.cs:line 23

I guess the problem is that NUnit can't get the connection string for the DataContext from web.config. What's the best way to get around this?

It works fine when I run the page, but the unit test fails in NUnit.

Best Answer

Copy your connection strings in the web.config project to app.config in your nunit test project.