tags:

views:

1229

answers:

5

Hi,

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?

Thanks, John

It works fine when I run the page but the unit test failes in NUnit

+2  A: 

The tack that I took was to mock out the data context. I use a factory to create the data context and inject it into the controller. If the factory is null (which is what happens when the parameterless constructor is called), it creates a default instance of the factory that connects to the database. In my unit tests, I use a factory that creates a fake data context that works in memory. I based my mock data context on code from this blog, though I've extended it to handle objects added to the database via entity sets.

If you don't want to go the mock route (and I would recommend it, though it will take some work up front). You can add an app.config file to your unit test project and put the connection strings for your data context in it.

tvanfosson
+1  A: 

This template and instructions helped me getting this up and running: http://johnnycoder.com/blog/2009/04/01/aspnet-mvc-test-template-project-for-nunit-and-moq/

Oskar Duveborn
A: 

The easiest is to have nUnit installed before you install MVC and then set up the test project when you create the MVC project. You then can create the config elements necessary to do what you are attempting.

NOTE: The better way to work with data is to mock it, which means you need to move your LINQ out of your controller, as you cannot easily invert control on a controller.

Gregory A Beamer
+1  A: 

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

Alan Jackson
works for me, thanks :)
melaos
+1  A: 

It is best not to access a database from your unit tests as this will result in tests that run so slowly that you'll stop bothering to run the tests. But if you do want to do this you can create an app.config file in your NUnit project and place your connection string there. Sometimes NUnit does not recognise this app.config file, but more details on how to configure it so it does can be found in the answers to "How to initialize ConnectionStrings collection in NUnit".

Martin Brown