如何使用mysql的操作结果测试此代码?
[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);
}
我试图写一些东西,但没有任何效果
[TestMethod]
public void TestMethod2()
{
HomeController homeController = new HomeController();
ActionResult result = homeController.SomeAction("12");
Assert.IsInstanceOfType(result, typeof(ViewResult));
}
我收到这样的错误
Assert.IsInstanceOfType 失败。预期类型:<Microsoft.AspNetCore.Mvc.RedirectToRouteResult>。实际类型:<Microsoft.AspNetCore.Mvc.JsonResult>
您的测试方法应如下所示。
[TestMethod]
public void TestMethod2()
{
HomeController homeController = new HomeController();
ActionResult result = homeController.SomeAction("12");
Assert.IsInstanceOfType(result, typeof(JsonResult));
}
您的方法正在返回 JsonResult,您正在尝试检查它是否为 ViewResult。这就是问题的原因。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句