我很难弄清楚如何在我的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://jonhilton.net/2016/05/23/3-ways-to-keep-your-asp-net-mvc-controllers-thin/
在您发布的示例中,您实际上没有任何逻辑-您基本上只是在进行数据库访问,并且假设您信任SqlCommand并且东西可以正常工作,那么没有什么可以真正测试的...在这种情况下,您可能想要一个集成测试,而不是,在那里你会建立一个空数据库,调用create方法,然后查询它所创建的数据库行你认为它应该有。但这在技术上并不是真正的“单元”测试。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句