gpt4 book ai didi

asp.net-mvc - 使用 NUnit 进行 ASP.NET MVC 单元测试

转载 作者:行者123 更新时间:2023-12-02 03:30:51 25 4
gpt4 key购买 nike

我一直在努力学习ASP.NET MVC使用 ASP.NET website 上发布的视频我在进行单元测试时遇到了问题。

我有一个非常简单的 Controller ,使用 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 中单元测试失败。

最佳答案

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

关于asp.net-mvc - 使用 NUnit 进行 ASP.NET MVC 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/789450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com