如何在 Asp.net MVC 中编写集成和系统测试
我的应用程序
我有一个如下所示的应用程序设计:
- Web应用程序层 - 带有使用 POCO 和调用服务的控制器和视图的 asp.net MVC 应用程序
- 服务层 - 使用 POCO 和调用存储库的业务流程
- 数据层 - 使用 POCO 和调用存储库的存储库以 EF 模型的形式与模型进行通信,EF 模型是同一层
- POCO 层的一部分 - 定义用于这些层之间相互通信的所有类
所以我的数据层对数据模型实现完全透明,因为上层不使用数据实体。
测试
据我所知,单元、集成和系统测试(与 Asp.net MVC 相关)是这样的:
- 单元测试 - 这个很简单。模拟解耦对象并将它们注入单元测试中,以便测试单元将使用它们
- 集成测试 - 应该创建一组生产功能单元并模拟其余单元:因此编写集成测试来测试控制器、服务和存储库集成,而无需实际使用生产数据库
- 系统测试 - 在所有层上运行测试而不进行任何模拟,这意味着我也必须使用生产(测试)数据库
问题
我可以轻松地了解如何编写单元测试以及系统测试,但我不知道如何编写集成测试?也许我对这些的看法是完全扭曲的,我根本不理解它们。
应该如何为 Asp.net MVC 应用程序编写集成和系统测试?
或者任何与此相关的 .net 应用程序?
一些有助于解释问题的代码
假设我有这样的类:
TaskController
调用TaskService
TaskService
调用TaskRepository
- < code>TaskRepository 在内部操作 EF 数据
所以这是我的(缩写的)类:
public class TaskController
{
private ITaskService service;
// injection constructor
public TaskController(ITaskService service)
{
this.service = service;
}
// default constructor
public TaskController() : this(new TaskService()) {}
public ActionResult GetTasks()
{
return View(this.service.GetTasks());
}
...
}
public class TaskService : ITaskService
{
private ITaskRepository repository;
// injection constructor
public TaskService(ITaskRepository repository)
{
this.repository = repository;
}
// default constructor
public TaskService() : this(new TaskRepository()) {}
public IList<Task> GetTasks()
{
return this.repository.GetTasks();
}
...
}
public class TaskRepository : ITaskRepository
{
public IList<Task> GetTasks()
{
// code that gets tasks from EF and converts to Task POCOs
}
...
}
单元测试很简单,看起来像这样:
public void UnitTest()
{
var mock = new Mock<ITaskService>();
// other code that mocks the service
TaskController controller = new TaskController(mock.Object);
// do the test
}
但是当涉及到集成测试时,我如何只模拟集成的某些部分。
public void IntegrationTest()
{
// no mocking at all
TaskController = new TaskController();
// do some testing
}
首先,我不能在这里模拟数据库吗?我可以模拟存储库并拥有真正的服务和控制器......
My application
I have an application design that looks like this:
- web application layer - asp.net MVC app with controllers and views that use POCOs and call services
- service layer - business processes that use POCOs and call repositories
- data layer - repositories that use POCOs and communicate with the model in the form of EF model which is part of this same layer
- POCO layer - defines all classes that are used for inter communication between these layers
So my data layer is completely transparent to data model implementation because upper layer don't use data entities at all.
Testing
As much as I understand unit, integration and system testing (with relation to Asp.net MVC) is this:
- unit testing - this one's easy. Mock decoupled objects and inject them in your unit test so tested unit will use them
- integration testing - should make a group of production functionality units and mock the rest: so writing integration tests that would test controller, service and repository integration without actually using production database
- system testing - run tests on all layers without any mocking, meaning I have to use production (test) database as well
Problem
I can easily see how to write unit tests as well as system tests, but I don't know how to write integration tests? Maybe my view of these is completely distorted and I don't understand them at all.
How should one write integration and system tests for an Asp.net MVC application?
Or any .net application for that matter?
Some code that helps explain the problem
Suppose I have classes like:
TaskController
calls intoTaskService
TaskService
calls intoTaskRepository
TaskRepository
manipulate EF data internally
So here are my (abbreviated) classes:
public class TaskController
{
private ITaskService service;
// injection constructor
public TaskController(ITaskService service)
{
this.service = service;
}
// default constructor
public TaskController() : this(new TaskService()) {}
public ActionResult GetTasks()
{
return View(this.service.GetTasks());
}
...
}
public class TaskService : ITaskService
{
private ITaskRepository repository;
// injection constructor
public TaskService(ITaskRepository repository)
{
this.repository = repository;
}
// default constructor
public TaskService() : this(new TaskRepository()) {}
public IList<Task> GetTasks()
{
return this.repository.GetTasks();
}
...
}
public class TaskRepository : ITaskRepository
{
public IList<Task> GetTasks()
{
// code that gets tasks from EF and converts to Task POCOs
}
...
}
Unit test is simple and would look like this:
public void UnitTest()
{
var mock = new Mock<ITaskService>();
// other code that mocks the service
TaskController controller = new TaskController(mock.Object);
// do the test
}
But when it comes to an integration test, how do I mock only certain parts of the integration.
public void IntegrationTest()
{
// no mocking at all
TaskController = new TaskController();
// do some testing
}
First of all I can't just mock database here? I could mock repository and have real service and controller though...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
集成测试应该测试组件之间的集成。单元测试测试单个组件的各个部分,而集成测试组件之间的交互,并且旨在实时工作。因此,集成测试将利用数据库和任何其他外部依赖项,最好在单元测试中模拟这些服务。
对我来说,系统测试是功能测试(使用 fit 之类的测试的另一个级别),或者通过 testcomplete 或 telerik 的 QA 工具等工具进行 ui 测试。
HTH。
Integration tests should test the integration between components. While unit tests test individual pieces of a single component, integration tests the interactions between components, and are meant to work live. So an integration test would utilize a database and any other external dependencies, where its best to mock these services in unit testing.
System test to me would be functional testing (another level of testing using something like fit), or ui testing through a tool like testcomplete or telerik's QA tool.
HTH.
不涉及 UI 的集成测试仍然可以用 NUnit、xUnit 等编写。
特别是对于 ASP.NET MVC(或任何 Web 应用程序),您可以使用 WatiN 或 Selenium 编写系统/集成使用 UI 进行测试。
您可能还想查看用于 T-SQL 单元测试的 TST 和 SpecFlow 如果您对 .NET 中的 BDD 感到好奇。
注意:我在用代码和具体情况的描述更新问题之前写了这篇文章。它不再真正解决这个问题,但希望对某人来说仍然有趣/有用。
Integration tests that don't involve the UI can still be written in NUnit, xUnit, etc.
For ASP.NET MVC in particular (or any web application), you can use WatiN or Selenium to write system/integration tests using the UI.
You might also want to look at T.S.T. for T-SQL unit testing, and SpecFlow if you are curious about BDD in .NET.
Note: I wrote this before the question was updated with code and a description of the specific situation. It doesn't really address the question anymore, but hopefully will still be interesting/useful to someone.
我刚刚回答了一个相关问题:集成测试实现。我认为它通过提出明确的方法解决了这个问题。
部分问题在于更高级别的集成测试变得过于复杂且很快。这是问题的本质,所以我更喜欢通过单元测试和根据所涉及的类进行集中集成测试,确保所有单独的部分按预期工作。
完成上述操作后,您只需确保单独的部分正确连接,因此我为此使用完整的系统测试。如果代码遵循 SOLID 和 DRY,则效果最佳。
I just replied to a related question: Integration tests implementation. I think it addressed the issue here, by presenting a clear approach to take.
Part of the issue is that higher level integration tests get too complex v. quickly. Its the nature of the problem, so I prefer to make sure all the separate pieces work as intended, through unit tests and focused integration tests depending on the class involved.
With the above in place, you only need to make sure the separate pieces are hooked correctly, so I use the full system tests for that. This has the best of its effect if the code follows SOLID and DRY.