如何写Asp.net MVC集成和系统测试 [英] How to write integration and system tests in Asp.net MVC

查看:182
本文介绍了如何写Asp.net MVC集成和系统测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序设计,看起来是这样的:

I have an application design that looks like this:


  • Web应用程序层 - asp.net MVC应用程序与控制器和使用波苏斯并调用服务的意见

  • 服务层 - 使用波苏斯和呼叫存储库的业务流程

  • 数据层 - 使用波苏斯并与EF模型的形式的模型,它是本相同层的一部分进行通信的存储库

  • POCO层 - 定义了用于
  • 这些层之间间通信的所有类
  • 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.

虽然我理解单元,集成和系统测试(有关系Asp.net MVC)是这样的:

As much as I understand unit, integration and system testing (with relation to Asp.net MVC) is this:


  • 单元测试 - 这一个容易。模拟解耦对象和注入他们在你的单元测试,以便检验单位将使用它们

  • 集成测试 - 应该做一团的生产功能单位和嘲笑,其余:所以写集成测试,将测试控制器,服务和存储库集成实际上并没有使用生产数据库

  • 系统测试 - 在没有任何嘲弄的所有层运行测试,这意味着我必须使用的生产(测试)数据库以及

我可以很容易地看到如何编写单元测试以及系统测试,但我不知道怎么写集成测试?也许我的这些观点是完全扭曲的,我根本不认识他们。

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.

应该怎么写一个Asp.net MVC应用程序?结果集成和系统测试
或与此有关的任何.NET应用程序?

How should one write integration and system tests for an Asp.net MVC application?
Or any .net application for that matter?

假设我有类,如:


  • TaskController 调用到 TaskService

  • TaskService 调用到 TaskRepository

  • TaskRepository 操纵EF内部数据

  • TaskController calls into TaskService
  • TaskService calls into TaskRepository
  • 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...

推荐答案

集成测试应该测试组件之间的融合。虽然单元测试测试单个组件的各个部分,集成测试组件之间的交互,以及是为了工作现场。因此,一个集成测试将利用数据库​​和任何其他外部依赖,在其最好的单元测试嘲笑这些服务。

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.

系统测试对我来说将是功能测试(使用类似配合测试的另一个级别),或者UI测试通过像testcomplete或Telerik的质量保证工具的工具。

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.

心连心。

这篇关于如何写Asp.net MVC集成和系统测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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