C# 如何在ASP.NET内核中使用mysql进行单元测试、actionresult
如何在mysql中使用ActionResult来测试这段代码C# 如何在ASP.NET内核中使用mysql进行单元测试、actionresult,c#,unit-testing,asp.net-core,C#,Unit Testing,Asp.net Core,如何在mysql中使用ActionResult来测试这段代码 [HttpPost] public ActionResult SomeAction(string qrcode) { string connectionString = @"Server=localhost;Database=aspkladovaya;Uid=root;Pwd=qwerty98123;"; string result = ""
[HttpPost]
public ActionResult SomeAction(string qrcode)
{
string connectionString = @"Server=localhost;Database=aspkladovaya;Uid=root;Pwd=qwerty98123;";
string result = "";
try
{
using (MySqlConnection sqlCon = new MySqlConnection(connectionString))
{
try
{
int userID = Convert.ToInt32(qrcode);
sqlCon.Open();
MySqlDataAdapter sqlDa = new MySqlDataAdapter("UserFindByID", sqlCon);
sqlDa.SelectCommand.Parameters.AddWithValue("_qruserid", userID);
sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
DataTable dtbl = new DataTable();
sqlDa.Fill(dtbl);
for (int i = 0; i < 3; i++)
{
if (qrcode == dtbl.Rows[i][1].ToString())
{
user.QrUserId = qrcode;
result = dtbl.Rows[i][3].ToString();
}
else
{
result = error;
}
}
}
catch
{
}
}
}
catch
{
}
return Json(result);
}
我犯了这样的错误
Assert.IsInstanceOfType失败。所需类型:。实际类型:
您的测试方法应该如下所示
[TestMethod]
public void TestMethod2()
{
HomeController homeController = new HomeController();
ActionResult result = homeController.SomeAction("12");
Assert.IsInstanceOfType(result, typeof(JsonResult));
}
您的方法正在返回JsonResult,您正在尝试检查它是否为ViewResult。这就是问题的原因
[TestMethod]
public void TestMethod2()
{
HomeController homeController = new HomeController();
ActionResult result = homeController.SomeAction("12");
Assert.IsInstanceOfType(result, typeof(JsonResult));
}