使用 NUnit 进行 ASP.NET MVC 单元测试

发布于 2024-07-18 04:33:32 字数 1350 浏览 8 评论 0原文

我一直在尝试使用 < 上发布的视频来学习 ASP.NET MVC a href="http://www.asp.net/learn/mvc-videos/video-421.aspx" rel="nofollow noreferrer">ASP.NET 网站 我遇到了问题单元测试。

我有一个非常简单的控制器,它使用 LINQ to SQL 来获取对象数组:

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

        return View();
    }

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

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 连接字符串。 解决这个问题的最佳方法是什么?

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

简单气质女生网名 2024-07-25 04:33:32

web.config 项目中的连接字符串复制到 nunit 测试项目中的 app.config 中。

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

瞄了个咪的 2024-07-25 04:33:32

我采取的策略是模拟数据上下文。 我使用工厂来创建数据上下文并将其注入控制器。 如果工厂为 null(这是调用无参数构造函数时发生的情况),它将创建连接到数据库的工厂的默认实例。 在我的单元测试中,我使用一个工厂来创建在内存中工作的虚假数据上下文。 我的模拟数据上下文基于 this 中的代码blog,尽管我已将其扩展为处理通过实体集添加到数据库的对象。

如果您不想走模拟路线(我会推荐它,尽管这需要预先做一些工作)。 您可以将 app.config 文件添加到单元测试项目中,并将数据上下文的连接字符串放入其中。

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.

兰花执着 2024-07-25 04:33:32

最好不要从单元测试中访问数据库,因为这会导致测试运行缓慢,以至于您将不再需要运行测试。 但如果您确实想这样做,您可以在 NUnit 项目中创建一个 app.config 文件并将连接字符串放在那里。 有时 NUnit 无法识别此 app.config 文件,但有关如何配置它的更多详细信息可以在“如何在 NUnit 中初始化 ConnectionStrings 集合”。

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

り繁华旳梦境 2024-07-25 04:33:32

最简单的方法是在安装 ASP.NET MVC 之前安装 NUnit,然后在创建 ASP.NET MVC 项目时设置测试项目。 然后,您可以创建执行您尝试的操作所需的配置元素。

注意:处理数据的更好方法是模拟它,这意味着您需要将 LINQ 移出控制器,因为您无法轻松反转控制器上的控制。

The easiest is to have NUnit installed before you install ASP.NET MVC and then set up the test project when you create the ASP.NET MVC project. You can then create the configuration 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文