ASP.NET MVC单元测试与NUnit的 [英] ASP.NET MVC unit tests with NUnit

查看:405
本文介绍了ASP.NET MVC单元测试与NUnit的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力学习 ASP.NET MVC使用上发布视频 ASP.NET网站和我遇到的一个问题做单元测试。

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.

我有一个使用的LINQ to SQL 得到的数组一个非常简单的控制器对象:

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();
    }

这在失败 NUnit的,出现以下错误:

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

我想这个问题是NUnit的不能从的web.config 获取DataContext的连接字符串。什么是解决这个问题的最好方法?

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?

当我运行该页面它工作正常,但在NUnit的单元测试失败。

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

推荐答案

的web.config 项目复制您的连接字符串为的app.config在NUnit测试项目

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

这篇关于ASP.NET MVC单元测试与NUnit的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆