Asp.Net Core MVC Web应用程序中的单元测试

第五

我很难弄清楚如何在我的MVC Web应用程序中进行单元测试。尽管这不是一个很难理解的概念,但是鉴于其多层体系结构,我无法确定是为此需要特定的框架还是需要遵循某些特定步骤。如果您能给我一些提示或代码片段,使我对应该实际做什么有一个想法,我将非常高兴。我将从您的项目中给您一个例子:

相位控制器

    public IActionResult Create()
    {

        return View();
    }

    [HttpPost]
    public IActionResult Create(Phase phase)
    {
        int releaseId = (int)TempData["id"];
        string connectionString = Configuration["ConnectionStrings:DefaultConnection"];
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            string sql = "CreatePhase";

            using (SqlCommand command = new SqlCommand(sql, connection))
            {
                command.CommandType = CommandType.StoredProcedure;

                // adding parameters
                SqlParameter parameter = new SqlParameter
                {
                    ParameterName = "@Name",
                    Value = phase.Name,
                    SqlDbType = SqlDbType.VarChar,
                    Size = 50
                };
                command.Parameters.Add(parameter);

                parameter = new SqlParameter
                {
                    ParameterName = "@ReleaseId",
                    Value = releaseId,
                    SqlDbType = SqlDbType.Int
                };
                command.Parameters.Add(parameter);

                connection.Open();
                command.ExecuteNonQuery();
                connection.Close();
            }
        }

        return RedirectToAction("Index", "Phase", new { id = releaseId });
    }

相模型

namespace Intersection.Models
{
    public class Phase
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int ReleaseId { get; set; }
       // public IEnumerable<Phase> phases { get; set; }
    }
}

以这两个为例-测试类的外观如何,以便正确测试Create()方法(以及POST方法)?

编辑:

在@Nkosi的回复之后,我添加了与另一个控制器不同的方法:

public IActionResult Create()
    {
        List<Intersection.Models.Environment> environmentList = new List<Intersection.Models.Environment>();

        string connectionString = Configuration["ConnectionStrings:DefaultConnection"];
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();

            string sql = "ReadEnvironments";
            SqlCommand command = new SqlCommand(sql, connection);
            command.CommandType = CommandType.StoredProcedure;

            using (SqlDataReader dataReader = command.ExecuteReader())
            {
                while (dataReader.Read())
                {
                    Intersection.Models.Environment environment = new Intersection.Models.Environment();
                    environment.Id = Convert.ToInt32(dataReader["Id"]);
                    environment.Name = Convert.ToString(dataReader["Name"]);
                    environmentList.Add(environment);
                }
            }

            ViewBag.Environments = environmentList;
            return View();
        }
    }

    [HttpPost]
    public IActionResult Create(Application application)
    {
        string connectionString = Configuration["ConnectionStrings:DefaultConnection"];
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            string sql = "CreateApplication";

            using (SqlCommand command = new SqlCommand(sql, connection))
            {
                command.CommandType = CommandType.StoredProcedure;

                // adding parameters
                SqlParameter parameter = new SqlParameter
                {
                    ParameterName = "@Name",
                    Value = application.Name,
                    SqlDbType = SqlDbType.VarChar,
                    Size = 50
                };
                command.Parameters.Add(parameter);

                parameter = new SqlParameter
                {
                    ParameterName = "@Environment",
                    Value = application.Environment,
                    SqlDbType = SqlDbType.Int
                };
                command.Parameters.Add(parameter);

                connection.Open();
                command.ExecuteNonQuery();
                connection.Close();
            }
        }

        return RedirectToAction("Index", "Application");
    }
米尔尼

就测试您自己的控制器本身(即它们返回视图和正确的数据到视图)而言,这很容易,并且在MSDN中已涉及:

https://docs.microsoft.com/zh-cn/aspnet/mvc/overview/older-versions-1/unit-testing/creating-unit-tests-for-asp-net-mvc-applications-cs

为了测试自己的逻辑(即在您的示例数据库调用),您将理想首先移动该逻辑之外的控制器动作,让你在隔离测试它,并没有得到夹杂了框架相关的东西。然后,您通常还可以将数据访问与实际逻辑分开(尽管在您的简单示例中,实际上没有要测试的任何逻辑,只有数据访问),然后您可以模拟返回的数据并确保您的逻辑按原样进行呈现受控数据集时期望。

尝试阅读以下内容:

一个单元应该如何测试.NET MVC控制器?

胖模型/瘦控制器与服务层

https://jonhilton.net/2016/05/23/3-ways-to-keep-your-asp-net-mvc-controllers-thin/

在您发布的示例中,您实际上没有任何逻辑-您基本上只是在进行数据库访问,并且假设您信任SqlCommand并且东西可以正常工作,那么没有什么可以真正测试的...在这种情况下,您可能想要一个集成测试,而不是,在那里你会建立一个空数据库,调用create方法,然后查询它所创建的数据库行你认为它应该有。但这在技术上并不是真正的“单元”测试。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何对ASP.NET Core Web应用程序(.Net Framework)进行单元测试?

使用 ASP.net 核心 MVC 应用程序中的列表为 View() 创建单元测试

如何在ASP.NET MVC多层应用程序中正确进行单元测试?

单元测试中的ActionResult问题无法断言POST返回值相等(asp.net core mvc web api)

在 ASP.NET Core 3 MVC Web 应用程序中设置路由

ASP Net Core MVC-在库中启动Web应用程序

如何在ASP.NET Core MVC中对RazorViewEngineOptions进行单元测试?

ASP.NET Core MVC应用程序设置

在 ASP.NET Core MVC 应用程序的数据记录表单中显示所选单元的适当字段

将 ASP.NET MVC Core 应用程序的 [部分] 发布到 Web 主机

Asp.Net Core 3.1 MVC Web应用程序使用SqlDateTime编译错误

使用 MVC 的 ASP.NET Core Web 应用程序/自动更新控制器

对.NET Core Web应用程序进行单元测试的正确方法是什么?

没有mvc的asp.net Core 2.0剃刀Web应用程序中的本地化

多次调用控制器函数会导致问题(ASP.NET MVC Web 应用程序中的 EF Core)

单元测试ASP.Net Web应用程序的App_Code

ASP NET Core MVC中的图表

ASP.NET Core MVC中的InputStream

在内存数据库提供程序中使用EF core进行asp.net core应用程序的单元测试的优势

将文件从ASP.NET MVC 4.6应用程序发布到ASP.NET Core Web API

如何在ASP.NET Core 1.1中对使用HttpContext的MVC控制器进行单元测试

如何对返回匿名对象的ASP.NET Core MVC控制器进行单元测试?

检索ASP Net Core 2.0 MVC中的应用程序版本

在ASP.NET MVC Core应用程序(RC2)中显示项目版本

在 ASP.NET Core MVC 中显示 React 应用程序的方法

User.Identity.Name在ASP.NET Core MVC应用程序中返回null

ASP.NET MVC Core 3.1应用程序中CSHTML更改,JS更改所需的生成

如何在asp.net core MVC应用程序中添加页面列表?

在ASP .NET CORE / MVC 6应用程序中设置Swagger时出现500错误