测试 ActionResult 时如何定义控制器上下文
How to define a Controller context when testing an ActionResult
我有一个 ActionResult,它在 EntityFramework 和 Epplus 的 MVC 5 项目中始终如一地工作。单击视图上的操作 link 会触发此 ActionResult,它将所选模型发送到新的 Excel 文档。
我正在学习在 Visual Studio 2013 年对我的 MVC 代码进行单元测试(使用 Nuget Xunit 包),我想我会从小做起,通过做相当于通过断言 ActionResult 不为空来对 ActionResult 进行 hello world 测试。
测试失败,响应如下:"System.InvalidOperationException : No connection string named 'StudentContext' could be found in the application config file."
我明白错误消息的含义,但我的问题是如何在测试项目中正确定义控制器上下文。我是不是遗漏了一些简单明了的东西,比如定义上下文变量,还是我的做法完全错误?
这是我用来测试 ActionResult 的代码块。
using StudentProject.Controllers;
using System.Web.Mvc;
using Xunit;
namespace StudentProject.Tests.Controllers
{
public class StudentRosterControllerTest
{
[Fact]
public void ExportToExcel_IsNotNull()
{
// Arrange
StudentRostersController controller = new StudentRostersController();
ActionResult ExcelExport;
// Act
ExcelExport = controller.ExportToExcel();
// Assert
Assert.NotNull(ExcelExport);
}
}
}
这是我正在测试的代码。它是一个自动脚手架控制器,隐藏了自动生成的 crud 方法,并显示了要测试的单个方法。
using OfficeOpenXml;
using StudentProject.Models;
using System.Collections;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;
namespace StudentProject.Controllers
{
public class StudentRostersController : Controller
{
private StudentContext db = new StudentContext();
// Auto-scaffolded CRUD methods not shown
// This ActionResult exports the StudentRoster model
// to a fresh Excel file.
public ActionResult ExportToExcel()
{
IEnumerable<StudentRoster> query = db.StudentRosters.AsEnumerable();
using (var excelFile = new ExcelPackage())
{
ExcelWorksheet worksheet =
excelFile.Workbook.Worksheets.Add("Sheet1");
worksheet.Cells["A1"].LoadFromCollection(Collection: query,
PrintHeaders: true);
// Results in file downloaded to user's default
// "My Downloads" folder.
return File(excelFile.GetAsByteArray(),
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"Export.xlsx");
}
}
}
}
System.InvalidOperationException : No connection string named
'StudentContext' could be found in the application config file."
确保测试项目的 app.config
文件具有适合您的 EF 的连接字符串设置。
测试项目app.config
<connectionStrings>
<add name="StudentContext" connectionString="..." providerName="System.Data.EntityClient" />
</connectionStrings>
how I properly define the Controller context in a Testing project
您的测试应该尝试复制 运行 时间环境。提供测试 SUT 所需的最低要求。
namespace StudentProject.Tests.Controllers
{
public class StudentRosterControllerTest
{
[Fact]
public void ExportToExcel_IsNotNull()
{
// Arrange
StudentRostersController controller = new StudentRostersController();
controller.ControllerContext = new ControllerContext() {
Controller = controller,
//...other properties needed for test
};
// Act
var actionResult = controller.ExportToExcel();
// Assert
Assert.NotNull(actionResult);
}
}
}
鉴于您正在连接到您的实际数据库,那么这将被视为更多的集成测试。
我建议抽象化您的数据访问,以便您可以在单元测试中对其进行模拟。
我有一个 ActionResult,它在 EntityFramework 和 Epplus 的 MVC 5 项目中始终如一地工作。单击视图上的操作 link 会触发此 ActionResult,它将所选模型发送到新的 Excel 文档。
我正在学习在 Visual Studio 2013 年对我的 MVC 代码进行单元测试(使用 Nuget Xunit 包),我想我会从小做起,通过做相当于通过断言 ActionResult 不为空来对 ActionResult 进行 hello world 测试。
测试失败,响应如下:"System.InvalidOperationException : No connection string named 'StudentContext' could be found in the application config file."
我明白错误消息的含义,但我的问题是如何在测试项目中正确定义控制器上下文。我是不是遗漏了一些简单明了的东西,比如定义上下文变量,还是我的做法完全错误?
这是我用来测试 ActionResult 的代码块。
using StudentProject.Controllers;
using System.Web.Mvc;
using Xunit;
namespace StudentProject.Tests.Controllers
{
public class StudentRosterControllerTest
{
[Fact]
public void ExportToExcel_IsNotNull()
{
// Arrange
StudentRostersController controller = new StudentRostersController();
ActionResult ExcelExport;
// Act
ExcelExport = controller.ExportToExcel();
// Assert
Assert.NotNull(ExcelExport);
}
}
}
这是我正在测试的代码。它是一个自动脚手架控制器,隐藏了自动生成的 crud 方法,并显示了要测试的单个方法。
using OfficeOpenXml;
using StudentProject.Models;
using System.Collections;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;
namespace StudentProject.Controllers
{
public class StudentRostersController : Controller
{
private StudentContext db = new StudentContext();
// Auto-scaffolded CRUD methods not shown
// This ActionResult exports the StudentRoster model
// to a fresh Excel file.
public ActionResult ExportToExcel()
{
IEnumerable<StudentRoster> query = db.StudentRosters.AsEnumerable();
using (var excelFile = new ExcelPackage())
{
ExcelWorksheet worksheet =
excelFile.Workbook.Worksheets.Add("Sheet1");
worksheet.Cells["A1"].LoadFromCollection(Collection: query,
PrintHeaders: true);
// Results in file downloaded to user's default
// "My Downloads" folder.
return File(excelFile.GetAsByteArray(),
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"Export.xlsx");
}
}
}
}
System.InvalidOperationException : No connection string named 'StudentContext' could be found in the application config file."
确保测试项目的 app.config
文件具有适合您的 EF 的连接字符串设置。
测试项目app.config
<connectionStrings>
<add name="StudentContext" connectionString="..." providerName="System.Data.EntityClient" />
</connectionStrings>
how I properly define the Controller context in a Testing project
您的测试应该尝试复制 运行 时间环境。提供测试 SUT 所需的最低要求。
namespace StudentProject.Tests.Controllers
{
public class StudentRosterControllerTest
{
[Fact]
public void ExportToExcel_IsNotNull()
{
// Arrange
StudentRostersController controller = new StudentRostersController();
controller.ControllerContext = new ControllerContext() {
Controller = controller,
//...other properties needed for test
};
// Act
var actionResult = controller.ExportToExcel();
// Assert
Assert.NotNull(actionResult);
}
}
}
鉴于您正在连接到您的实际数据库,那么这将被视为更多的集成测试。
我建议抽象化您的数据访问,以便您可以在单元测试中对其进行模拟。